Helpful Information
 
 
Category: MySQL
Multiple selectboxes loading (dinamical) help

Hi,

I would like to implement a set o 3 selectboxes (or combo boxes) that would load their optionv values dynamically from a MySQL database using Javascript, PHP and Mysql.

This would be more or less like this:

User selects State -> which would load County selectbox with all Counties belonging to the selected State.

Then...

User select County -> which would load City selectbox with all cities belonging to the selected county.

So we would have 3 selectboxes to load: State, County and City.

Anyone has an idea of how may I do it using also Javascript to load Dinamically?

Thanks in advance for any help.

MarioPro

I've done this at just about every gig I've had...it's cool. What you have to do is have the 3 <select> menus in html, with only the first, states, being populated directly by the PHP. The other 2 are blank. The PHP also builds the multidimensional arrays which, will have the qualities as follows:

myarray["New York"]["Orange"]["Albany"];
myarray["New York"]["Orange"]["Schenectady"];
myarray["New York"]["Jefferson"]["Watertown"];

Each state is declared an array, as is each county. Then use the new Option javascript to run through the arrays to populate onChange of the state select menu.

HTH,
Mike

Hi,

Thanks for your input.

The fact is that I'm not confortable with Javascript and that seems to be one big problem on building that.

Would it be to ask to much if you could just give me an example of how I would do it using the following example (almost real) ?

Thanks in advance for your good colaboration.

Here are the selects I was thinking to use:

<SELECT NAME="c[county]">
<OPTION VALUE="Any">Any</OPTION>
<?
$query = "SELECT * FROM countys WHERE county='$countyID' ORDER BY county ASC";
$result = mysql_query($query);
while($rowCnt = mysql_fetch_array($result)){
echo '<OPTION VALUE="'.$rowC['ID'].'" ';
if($rowCnt['ID'] == $c['county']){
echo ' SELECTED ';
}
echo '>'.$rowCnt['county'].'</OPTION>'."\n";
}
?>
</SELECT>


Now, I would like to go on my database to populate anothet dependent selectbox after user's input on the former:


<SELECT NAME="c[city]">
<OPTION VALUE="Any">Any</OPTION>
<?
$query = "SELECT * FROM cities WHERE county_ID='$countyID' ORDER BY city ASC";
$result = mysql_query($query);
while($rowCc = mysql_fetch_array($result)){
echo '<OPTION VALUE="'.$rowCc['ID'].'" ';
if($rowCc['ID'] == $c['city']){
echo ' SELECTED ';
}
echo '>'.$rowCc['city'].'</OPTION>'."\n";
}
?>
</SELECT>

And just on more dependent for the islands stuff:

<SELECT NAME="c[island]">
<OPTION VALUE="Any">Any</OPTION>
<?
$query = "SELECT * FROM islands WHERE city_ID='$cityID' ORDER BY island ASC";
$result = mysql_query($query);
while($rowI = mysql_fetch_array($result)){
echo '<OPTION VALUE="'.$rowI['ID'].'" ';
if($rowI['ID'] == $c['island']){
echo ' SELECTED ';
}
echo '>'.$rowI['island'].'</OPTION>'."\n";
}
?>
</SELECT>










privacy (GDPR)