dongweiben5229 2013-05-06 21:05
浏览 55
已采纳

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

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. 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. 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;
?>
  • 写回答

2条回答 默认 最新

  • doushi3189 2013-05-06 21:59
    关注

    Don't forget to quote .$r['mobname'].

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

    Your $_POST tests only .

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

    What about if $_POST["filter"] is NOT set ?
    Your code did not take care of this fact. He always runs through the section $menu2.

    Your $_POST is as the name implies, of a post. To evaluate a $_POST must be done a 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"])) {

     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.

    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

    you forget to quote that to

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

    right

     $options .="<option value=\"". $row['compName'] ."\" >" . $row['compName'] .    "</option>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog