/    Sign up×
Community /Pin to ProfileBookmark

Drop Down list results

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($_GET[“state”]))
{
$state = $_GET[“state”];
}

if(isset($_GET[“county”]) && is_numeric($_GET[“county”]))
{
$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

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Hazit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...