我想根据另一个使用 ajax 的输入填充下拉列表。但是有一个死胡同,无法弄清楚问题所在。它使用公司名称打开该公司制造的手机的下拉列表。
这是源 php 文件:
<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>
这是为了查询手机名称而引用的 getmob php 文件:
<?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;
?>
最后这是下拉列表中使用的javascript:
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:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
我错过了什么?请帮忙。