I want to have a dynamic dropdown box whose entries would depend on the selection of an entry in the first dropdown box. BUT the second dropdown box should not reload, only the entries inside should get refreshed or reloaded.
i have a piece of code to create a dynamic dropdown box from w3schools.com but it has the problem of reloading the second dropdown box as well. Please see my code:
So, as shown above in the <div id="txtHint"><b > </b></div> area i want to have a pre-present dropdown box and when the uses selects an entry in the first dropdown box, the second dropdown box entries should be changed but not the dropdown box as a whole. Please advice me!
i have a piece of code to create a dynamic dropdown box from w3schools.com but it has the problem of reloading the second dropdown box as well. Please see my code:
Code:
//index.php
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp;function showUser(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getdept.php";
url=url+"?c="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body><form>
Select a User:
<select name="category" onchange="showUser(this.value)">
<option value="Ministry">Ministry</option>
<option value="Department">Department</option>
<option value="District">District</option>
<option value="Corporation">Corporation</option>
<option value="NGOs">NGOs</option>
<option value="Others">Others</option>
</select>
</form><p>
<div id="txtHint"><b> </b></div>
</p></body>
</html>
//getdept.php
<?php
$c=$_GET["c"];
$con = mysql_connect('localhost', 'root', 'root');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$sql="SELECT * FROM tblagency WHERE category = '".$c."'";
$result = mysql_query($sql);
echo "<select name=\"agency\">";
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row['id'].">".$row['agency']."</option>";
}
echo "</select>";
mysql_close($con);
?>
Comment