从数据库中获取的数据但此表项的ID未显示 [英] data fetched from database but id of this this table item not showing

查看:262
本文介绍了从数据库中获取的数据但此表项的ID未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经更新我的问题,可以轻松解释您的问题。可以这么说,
我有什么问题和什么问题?
i代码产品表单getcatlist.php帮助我从sql数据库获得价值并在产品页面中显示产品表单快照 产品表单图片



产品表单代码

 < section class =main-section> 
< article class =prod-item>
< h3>产品表格< / h3>
< form action =addproducts.phpmethod =getname =product_form>
< section class =select-box>
< label for =category>类别< / label>
<?php require_once(getcatlist.php); ?>
< input type =hiddenname =cat_idvalue =<?php echo $ _GET ['cat_id'];?> >
< / section>

< / form>
< / article>
< / section>

我的javascript在产品表单页面用于创建查询字符串代码

 < script type =text / javascript> 
函数showCategory(str)
{
document.product_form.action =addproducts.php?cat_id =+ str;
}
< / script>

** getcallist.php从数据库获取数据并显示在选择标记中的页面代码**

 <?php 

error_reporting(E_ALL ^ E_NOTICE);

require_once(../ data_connect.php);
$ error_msg =;
$ cat_id =;
$ catsql =SELECT Cat_id,CategoryName FROM Cat_id asc;按顺序排序;;
$ cat_query = mysqli_query($ connect,$ catsql);
$ numrows = mysqli_num_rows($ cat_query);
$ cat_id = $ _GET ['cat_id'];
if($ numrows == 0)
{
$ error_msg =找不到类别;

else {
echo< select name = \category \id = \category \onchange = \showCategory(this.value)\\ \\>;
echo< option value = \null \>选择您的类别< / option>;
while($ catrow = mysqli_fetch_assoc($ cat_query))
{
if($ catrow ['Cat_id'] == $ cat_id){
echo< option value = \。$ catrow ['Cat_id']。\selected = \selected\>。$ catrow ['CategoryName']。< / option>;

else {
echo< option value = \。$ catrow ['Cat_id']。\>。$ catrow ['CategoryName'] 。 < /选项> 中;
}
}
echo< / select>;
}
?>

这里是我的ajax代码,当我点击下拉列表时, strong>

 < script type =text / javascript> 
/ ** / $(document).ready(function(){
$(#category)。change(function(){
var code = $(this).val ();

if(code == 1)
{
$('。main-section')。load('addconsoles.php');


else if(code == 2)
{
$('。main-section')。load('addaccessories.php');
}
else if(code == 3)
{
$('。main-section')。load('addgames.php');
}
} );
});

< / script>

好的我想创建一个类别列表的查询字符串,当我点击下拉菜单中的任何类别选项时列出它不会在URL中显示我的任何东西。这是我面临的最大问题,也是当我尝试设置并获得php oop类中的值(需要在产品表单页面中)时,它不会设置和获取回显时的值。
请帮我解决这个问题,谢谢

解决方案

我认为你对HTML表单的工作原理有些误解。

你得到的代码不会像你期望的那样工作,因为你已经定义了 cat_id 字段两次在您的表单中 - 一次在您创建的隐藏字段中,以及当您通过JavaScript更改表单的操作方法时。但是,当您的JavaScript运行时,它会修改操作字符串,但隐藏字段不会更改,并且当您提交表单时,浏览器将从隐藏字段中提交值,并忽略手动添加到行动方法。这只是在这种情况下的默认行为,以解决您创建的歧义。



因此,如果在第一次加载页面时,没有值在 cat_id (这看起来很可能,看着你的代码),那么 cat_id 的值不会被发送到服务器当表单被提交时。



然而,我上面描述的所有代码实际上是不必要的。您可以直接使用下拉列表的值作为 cat_id 。只需更改< select> 的name参数,如下所示:

  echo'< select name =cat_idid =category>'; 

会使 cat_id 参数更改为任意值在表单提交时,它位于下拉列表的选定值中。



您不需要showCategory函数或隐藏字段。

所以表单可以是

 < form action =addproducts.phpmethod =getname =product_form> 
< section class =select-box>
< label for =category>类别< / label>
<?php require_once(getcatlist.php); ?>
< / section>
< / form>

不带隐藏字段,并且可以完全删除showCategoryJavaScript函数。



注意从这段代码看来,你无法将这个addproducts表单提交给服务器。目前尚不清楚您是否有其他JavaScript控制此功能,并且是基于其他事件触发的,或者您是否需要向表单添加提交按钮。无论采用哪种方式,在您提交表单之前,都不会向服务器发送任何消息,或者为用户提供一种方式。



我希望这很有用,请评论,如果你还发现有什么不清楚的。


Hi i have update my question to explain you my problem easily.Okay so, What i wnant and whats wrong with me? i code a product form where i require getcatlist.php which help me get value from sql database and show in product page, Snapshot of product form product form image

Product form code

    <section class="main-section">
    <article class="prod-item">
        <h3>Product Form</h3>
        <form action="addproducts.php" method="get" name="product_form">    
            <section class="select-box">
                <label for="category">Category</label>
                <?php   require_once("getcatlist.php"); ?>  
                <input type="hidden" name="cat_id" value="<?php echo $_GET['cat_id']; ?>" >
            </section>

        </form> 
    </article>
</section>

my javascript in product form page used to create query string code

<script type="text/javascript">
    function showCategory(str)
    {   
        document.product_form.action="addproducts.php?cat_id="+str;             
    }       
</script>

**getcallist.php page code which fetch data from databse and show in select tag **

<?php 

error_reporting(E_ALL ^ E_NOTICE);   

require_once("../data_connect.php");
$error_msg = "";
$cat_id = "";
$catsql = "SELECT Cat_id,CategoryName FROM category order by Cat_id asc; ";
$cat_query = mysqli_query($connect,$catsql);
$numrows = mysqli_num_rows($cat_query);
$cat_id = $_GET['cat_id'];
    if($numrows == 0)
    {
        $error_msg= "No Categories Found";
    }
    else{
                echo "<select name=\"category\" id=\"category\" onchange=\"showCategory(this.value)\" >"; 
                echo "<option value=\"null\">Select your category</option>";
        while($catrow = mysqli_fetch_assoc($cat_query))
            {
                if ($catrow['Cat_id'] == $cat_id){
                    echo "<option value=\"".$catrow['Cat_id']."\" selected=\"selected\">".$catrow['CategoryName']."</option>";
                }
                else{
                                echo "<option value=\"".$catrow['Cat_id']."\">".$catrow['CategoryName']."</option>";
                }
            } 
                echo "</select>";
    }
 ?>

here is my ajax code which move to another page when i click on dropdown list

    <script type="text/javascript">
        /**/$(document).ready(function(){
            $("#category").change(function(){
                var code = $(this).val();

                if(code == 1)
                {
                    $('.main-section').load('addconsoles.php');

                }
                else if(code == 2)
                {
                    $('.main-section').load('addaccessories.php');
                }
                else if(code == 3)
                {
                    $('.main-section').load('addgames.php');
                }
            });
        });

</script>

okay i wants to create a query string of category list, when i click on any category option in dropdown list it wont show me any thing in url. This is the big problem i face and also when i try to set and get value in php oop class(require in product form page) it won't set and get when echo it. Please help me out from this problem, Thanks

解决方案

I think you have somewhat mis-understood how HTML forms work.

The code you've got will not work as you expect because you have got the cat_id field defined twice within your form - once in the hidden field you created, and again when you alter the action method of the form via the JavaScript. However when your JavaScript runs, it modifies the "action" string, but the hidden field will not change, and when you submit the form, the browser will submit the value from the hidden field, and ignores the value you manually added to the "action" method. This is just the default behaviour in such a situation, in order to resolve the ambiguity you created.

Therefore if at the time the page was first loaded, there was no value in cat_id (which seems likely, looking at your code), then no value for cat_id will be sent to the server when the form is submitted.

However, all of the code I described above is actually unnecessary. You can directly use the value of the dropdownlist as the cat_id. Simply changing the "name" parameter of your <select>, as follows:

echo '<select name="cat_id" id="category">'; 

will make the cat_id parameter change to whatever is in the selected value of the dropdown when the form is submitted.

You do not need the "showCategory" function, or the hidden field.

So the form can be

<form action="addproducts.php" method="get" name="product_form">    
  <section class="select-box">
    <label for="category">Category</label>
    <?php   require_once("getcatlist.php"); ?>  
  </section>
</form>

without the hidden field, and the "showCategory" JavaScript function can be deleted entirely.

N.B. From this code it seems you have no way to submit this "addproducts" form to the server. It's not clear whether you have some other JavaScript elsewhere which controls this and is triggered based on some other event, and/or whether you need to add a "submit" button to the form. Either way, nothing will be sent to the server until you cause the form to be submitted, or provide a way for the user to cause it.

I hope this is useful, please comment if you still find anything unclear.

这篇关于从数据库中获取的数据但此表项的ID未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆