I am having a little problem with drop down lists and am hoping for a little help. In searching I have managed to come up with two drop down lists dependent of each other to search my mysql tables. What I am trying to do is get results in a table based on the selection of the second drop down list.
My first list pulls all states in the US and the second will pull all counties. I have a third table with all my members. I would like to be able to search for all members in my database based on the county they live in.
In the table result, I would like to be able to show their name, address, phone number and email.
Here is the catch. The state table has state_id and state_name the county has county_id and county_name as well as state_id which allows me to tie the two together. The third table has the state name and county name but no id.
Here is what I have for a code that allows me the drop down lists.
<?php
$state = $county = $city = null; //declare vars
$conn = mysql_connect(“host”, “username”, “password”);
$db = mysql_select_db(‘database’,$conn);
if(isset($_GET[“state”]) && is_numeric($
{
$state = $_GET[“state”];
}
if(isset($_GET[“county”]) && is_numeric($
{
$county = $_GET[“county”];
}
?>
<script language=”JavaScript”>
function autoSubmit()
{
var formObject = document.forms[‘theForm’];
formObject.submit();
}
</script>
<form name=”theForm” method=”get”>
<!– state SELECTION BASED ON city VALUE –>
<?php
?>
<select name=”state” onChange=”autoSubmit();”>
<option value=”state”>Select State</option>
<?php
//POPULATE DROP DOWN MENU WITH STATES
$sql = “SELECT * FROM cat_state”;
$states = mysql_query($sql,$conn);
while($row = mysql_fetch_array($states))
{
echo (“<option value=”$row[state_id]” ” . ($state == $row[“state_id”] ? ” selected” : “”) . “>$row[state]</option>”);
}
?>
</select>
<?php
?>
<br><br>
<?php
if($state != null && is_numeric($state))
{
?>
<select name=”county” onChange=”autoSubmit();”>
<option value=”county”>Select County</option>
<?php
//POPULATE DROP DOWN MENU WITH COUNTIES FROM A GIVEN STATE
$sql = “SELECT * FROM cat_county WHERE state_id = $state ORDER BY county”;
$counties = mysql_query($sql,$conn);
while($row = mysql_fetch_array($counties))
{
echo (“<option value=”$row[county_id]” ” . ($county == $row[“county_id”] ? ” selected” : “”) . “>$row[county]</option>”);
}
?>
</select>
</form>
<?php
}
?>
Any help would be greatly appreciated