douwu5009 2013-05-08 00:00
浏览 47
已采纳

如何使用ajax,php和html填充另一个下拉列表

i want to populate a dropdownlist based on an input from another using ajax. But have head a deadend and can't figure out the problem. It uses company names to open up a dropdown list of mobile phone made by that company.

This is the source php file:

 <body>
 <div class="ex">
 <form id="mob1" >
  <label></br> Select Mobile 1:</label>
  <label></br> --Company-- <label>
  <select id="d1mob1" onchange="getmob(this.value)">
  <?php 
    $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>";
    }
    echo $options;
  ?>
  </select>
  <label class="ey" name="mlabel"></br> --Mobile--<label>

  <select id="d2mob1" >
  <?php 
    if($q2=mysqli_prepare($con,"SELECT DISTINCT mobname FROM umobile WHERE compName=? "))
    {
        mysqli_stmt_bind_param($q2,"s",$comp);
        mysqli_stmt_execute($q2);
        mysqli_stmt_bind_result($q2,$r);
    }   

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

</form>
</div>

 </body>

This is the getmob php file being referred to inorder to query the mobile name :

  <?php 
$name="";
$comp=$_GET['d1mob1'];
if($q2=mysqli_prepare($con,"SELECT DISTINCT mobname FROM umobile WHERE compName=? "))
{
    mysqli_stmt_bind_param($q2,"s",$comp);
    mysqli_stmt_execute($q2);
    mysqli_stmt_bind_result($q2,$r);
}   

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

and finally this is javascript being used in the dropdownlist:

  function getmob(compname)
  {
    var strURL="getmob.php?d1mob1="+compname;
    var req = getXMLHTTP();
    if (req)
    {
     req.onreadystatechange = function()
     {
       if (req.readyState == 4) // only if "OK"
       {
         if (req.status == 200)
         {
           document.getElementById('mlabel').innerHTML=req.responseText;
         } else {
          alert("There was a problem while using XMLHTTP:
" + req.statusText);
        }
      }
    }
   req.open("GET", strURL, true);
   req.send(null);
  }
  }

What am i missing ? Please help.

  • 写回答

1条回答 默认 最新

  • dougua3706 2013-05-08 00:31
    关注

    document.getElementById('mlabel')??? r you want to put the responseText in d2mob1?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算