Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •