简体   繁体   English

如何通过从另一个下拉列表中选择值来填充下拉列表?

[英]How do i populate a dropdown list by selecting the value from another dropdown list?

I have been trying to populate the contents of one drop down list with the selection of an item in another, using php,html and mysql database and Its proved to be a lot harder than i thought for me. 我一直在尝试使用php,html和mysql数据库填充一个下拉列表的内容,并选择另一个条目,事实证明,这比我想的要难得多。 I'm trying to get one dropdown list show names of all mobile phones based on company selections in the first drop down list. 我试图根据第一个下拉列表中的公司选择,获得一个下拉列表,显示所有手机的名称。

The code i've provided below has no errors, but the 2nd dropdown list is not getting populated. 我在下面提供的代码没有错误,但是没有填充第二个下拉列表。 the variable '$submittedValue' is not getting the right value and hence '$name' also has no content in it. 变量“ $ submittedValue”没有获得正确的值,因此“ $ name”中也没有内容。 I can't seem to be able to figure out a reason for this. 我似乎无法找出原因。

what i've done is, to try to use the selected option in the first drop down list, put that into a variable and pass that as a parameter to the 2nd query. 我所做的是,要尝试使用第一个下拉列表中的选定选项,请将其放入变量中,并将其作为参数传递给第二个查询。

I solution to this would be greatly appreciated. 我对此的解决方案将不胜感激。

<?php

 $con=mysqli_connect("localhost","root","","dbmobiles");

 // Check connection
 if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 $options = '';
 $submittedValue = '';
 $name=" ";

 $q1=mysqli_query($con,"select distinct compName from umobile order by compName ASC");
 while($row = mysqli_fetch_array($q1)) {
    $options .="<option value=". $row['compName'] ." >" . $row['compName'] .    "</option>";
 }

 $menu1 = "<form id='filter' name='filter' method='post' action=' '>    
           <p><label>Select Company</label></p>
           <select name='filter' id='filter'>
           " . $options . "
           </select>
          </form>";

 if (isset($_POST["filter"])) {
    $submittedValue = $_POST["filter"];
 }
 echo $menu1;

 if ($q2=mysqli_prepare($con,"SELECT DISTINCT mobname FROM umobile WHERE compName=? "))
 {
    mysqli_stmt_bind_param($q2,"s",$submittedValue);
    $s=$submittedValue;
    mysqli_stmt_execute($q2);
    mysqli_stmt_bind_result($q2,$r);
 }  

 while(mysqli_stmt_fetch($q2)) {
    $name .="<option>".$r['mobname']."</option>";
 }

 echo ".$name.";
 $menu2 = "<form id='moblist' name='moblist' method='post' action=' '>
            <p><label>Select Mobile</label></p>
            <select name='moblist' id='moblist'>
            " . $name . "
            </select>
           </form>";

 echo $menu2;
?>

Don't forget to quote .$r['mobname']. 不要忘记引用.$r['mobname'].

$name .="<option value=\"".$r['mobname']."\" >".$r['mobname']."</option>";

Your $_POST tests only . 您的$ _POST仅测试。

if (isset($_POST["filter"])) {
    $submittedValue = $_POST["filter"];
}

What about if $_POST["filter"] is NOT set ? 那么如果$ _ POST [“过滤器”]是不是设置
Your code did not take care of this fact. 您的代码没有考虑到这一事实。 He always runs through the section $menu2. 他总是遍历$ menu2部分。

Your $_POST is as the name implies, of a post. 顾名思义,您的$ _POST是帖子。 To evaluate a $_POST must be done a post. 要评估$ _POST,必须完成一个发布。

 <input type="submit" name="Submit" value="Submit" />

for example 例如

 $menu1 = "<form id='filter' name='filter' method='post' action=' '>    
             <p><label>Select Company</label></p>
            <select name='filter' id='filter'>
             " . $options . "
            </select>
            <input type=\"submit\" name=\"Submit\" value=\"Send\" />
          </form>";

Put all the logic for the second menu in your code if (isset($_POST["filter"])) { if (isset($_POST["filter"])) {

 echo $menu1;

 if (isset($_POST["filter"])) {
    $submittedValue = $_POST["filter"];
      if ($q2= ....)
       {
         mysqli_stmt_bind_param($q2,"s",$submittedValue);
         $s=$submittedValue;
         mysqli_stmt_execute($q2);
         mysqli_stmt_bind_result($q2,$r);

         while(mysqli_stmt_fetch($q2)) {
         $name .="<option value=\"".$r['mobname']."\" >".$r['mobname']."</option>";
         }

         $menu2 = "<form id='moblist' name='moblist' method='post' action=' '>
            <p><label>Select Mobile</label></p>
            <select name='moblist' id='moblist'>
            " . $name . "
            </select>
           </form>";

        echo $menu2;
       }  
   }

Update: 更新:

You can not use $r['mobname'] with a bind. 您不能将$ r ['mobname']与绑定一起使用。

mysqli_stmt_bind_result($q2,$r);
....
while(mysqli_stmt_fetch($q2)) {
$name .="<option value=\"".$r['mobname']."\" >".$r['mobname']."</option>";

} }

Use it like . 像这样使用它。

mysqli_stmt_bind_result($q2,$r);
....
while (mysqli_stmt_fetch($q2)) {
$name .="<option value=\"".$r."\" >".$r."</option>";
}


mysqli_stmt_close($q2);

Update 2 更新2

you forget to quote that to 你忘了引用

 $options .="<option value=". $row['compName'] ." >" . $row['compName'] .    "</option>";

right

 $options .="<option value=\"". $row['compName'] ."\" >" . $row['compName'] .    "</option>";
$menu1 = "<form id='filter' name='filter_form' method='post' action=' '>    
           <p><label>Select Company</label></p>
           <select onchange='filter_form.submit()' name='filter' id='filter'>
           " . $options . "
           </select>
          </form>";

there is no submit button in your form , either place a submit button or try to submit on selection on dropdown values. 您的表单中没有提交按钮,可以放置一个提交按钮,也可以尝试根据下拉值进行选择。 Only then if($_POST['filter']) will be true and your variable will successfully get assignment 只有那时if($ _ POST ['filter'])为真,您的变量才能成功获得赋值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM