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.