Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

php dynamic select drop down menu

  • 02-08-2007 10:37AM
    #1
    Registered Users, Registered Users 2 Posts: 3,507 ✭✭✭


    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, Registered Users 2 Posts: 68,173 ✭✭✭✭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, Registered Users 2 Posts: 3,507 ✭✭✭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
Advertisement