I have build two drop downs (like state and city) by fetching the records of both drop downs from mysql database and am trying to build the tool in which, while selecting any value (i.e. any state) from first drop down, at that time in second drop down (in city) only those values (cities) under that value (state) selected in first drop down should be visible.
Here’s my code:
<tr>
<td id='hed'><span style="font-family:verdana,geneva,sans- serif">State</state></td>
<td>
<?php
$dbcon = mysql_connect("@ip","@username","@password");
if($dbcon)
{
mysql_select_db("@database", $dbcon);
}
else
{
die('error connecting to the database');
}
$qry = "select @value(state) from @tablename ";
$result = mysql_query($qry) or die(mysql_error());
$dropdown = "<select name='@valuename' id='officeItemList' style='cursor:pointer;cursor:hand;'>";
while($row = mysql_fetch_array($result))
{
$dropdown .= "\r\n<option value='{$row['@value']}' > {$row['@value']} </option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
mysql_close($dbcon);
?>
</td>
</tr>
<tr>
<td id='hed'><span style="font-family:verdana,geneva,sans-serif">City</span></td>
<td colspan="1">
<?php
$dbcon = mysql_connect("@ip","@username","@password");
if($dbcon)
{
mysql_select_db("@database", $dbcon);
}
else
{
die('error connecting to the database');
}
$qry = "select value2(city) from @tablename where ";
$result = mysql_query($qry) or die(mysql_error());
$dropdown = "<select name='@value2' id='officeItemList' style='cursor:pointer;cursor:hand;'>";
while($row = mysql_fetch_array($result))
{
$dropdown .= "\r\n<option value='{$row['@value2']}' > {$row['@value2']} </option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
mysql_close($dbcon);
?>
</td>
</tr>
You can use AJAX to fetch the cities for the selected state. Something like:
You can also return a json object (in which case don’t forget to add
dataType:"json") and make the transition to HTML in the client-side, i.e inside thesuccessfunction