Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php dynamic select drop down menu

Options
  • 02-08-2007 11:37am
    #1
    Registered Users Posts: 3,401 ✭✭✭


    Hi Lads,

    I've found a few of these online I guess I'm just wondering where I could find the most straightforward?

    I will basically have a parent select drop down menu, then a child select drop down menu and another below that?

    I would prefer to query the database after the selection has been made.

    Thanks
    Gary


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Javascript is really the man for this.

    You have two options -
    One is to load in all of the possible child options to javascript, grouped into arrays. Create an onChange() handler for the parent drop-down, which updates the contents of the child drop-down when the parent one changes.

    A neater (and cooler) way to do it is to use AJAX. When the parent drop-down changes, javascript goes back to the server and retrieves a list of child elements. You then load these into the child drop-down.

    There's a good AJAX primer on www.w3schools.com that should get you started quickly.


  • Registered Users Posts: 3,401 ✭✭✭randombar


    Thanks for introduction me to Ajax, a new language for me to explore, it seems class! Anyways I better now start my Ajax quesitons:

    How do I get the result of my database query to spit into the select options? I guess I wouldnt be 100% at divs

    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    etc
    etc

    Also how do I spit the results of my mysql query into a slectable drop down list like google suggestions?

    Any help will be appreciated.

    Gary


  • Closed Accounts Posts: 22,479 ✭✭✭✭philologos


    you could do it using a while loop.
    [PHP]
    $query = "select * from whatever";
    $result = mysql_query($query);
    $numrows = mysql_num_rows($result);
    $i = 0;
    while ($i < $numrows) {
    $field = mysql_result($result, $i, "field");
    echo "<option value='$i'>$field</option>";
    $i++;
    }
    ?>[/php]


Advertisement