Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-01-2002, 10:54 AM   PM User | #1
MarioPro
New Coder

 
Join Date: Jul 2002
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
MarioPro is an unknown quantity at this point
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
MarioPro is offline   Reply With Quote
Old 08-01-2002, 09:44 PM   PM User | #2
storemike
New to the CF scene

 
Join Date: Aug 2002
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
storemike is an unknown quantity at this point
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
storemike is offline   Reply With Quote
Old 08-01-2002, 09:54 PM   PM User | #3
MarioPro
New Coder

 
Join Date: Jul 2002
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
MarioPro is an unknown quantity at this point
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>
MarioPro is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:15 AM.


Advertisement
Log in to turn off these ads.