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 15 of 15

Thread: Drop down Menu

  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Drop down Menu

    Hi all,

    Would anyone have any ideas about how i would create a drop down menu, whose options are taken directly from a table on a mysql database?

    Thanks,
    Gismo

  • #2
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $link 
    mysql_connect("localhost","username","password") or die(mysql_error()); /* connect to db */
    mysql_select_db("db") or die(mysql_error()); /* select db to work from*/
    $query "SELECT id,option FROM menu_table ORDER by ID ASC"/* set query */
    $result mysql_query($query) or die(mysql_error()); /* run query */
        
    if($result){ /* if sucessful */
            
    if(mysql_num_rows($result) == "0"){ /* check for no information */
                
    echo "No information in database table!";
              }
        else{ 
    /* if information is present in table, proceed to make the form */                       
            
    echo '<form action="whatever.php" name="menu" method="POST">';
            echo 
    '<select size="1" name="menu">'/* echo outside of the while() because we don't want a lot of different drop down boxes! */
                
    while($row mysql_fetch_assoc($result)){
                    echo 
    '<option>'.$row['option'].'</option>'/* each individual result will make an option */
                    

            echo 
    '</select>'/* end the select box */
            
    echo '</form>';
           }
          }
    ?>
    Should work! Hopefully, i got all of my '}'s in there - I typed it fast. lol.

    Hope this helps!
    Last edited by SDP2006; 05-03-2004 at 05:10 PM.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #3
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi,

    Thank you for the reply, the code brings up a drop down menu, but all of the options are showing up blank in the drop down menu. It is clear that it is getting all the values from the database but is showing them blank. Any suggestions?

    Thanks,
    Gismo

  • #4
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you give a link to the page? It would help If I could see the HTML source of the page to see whats going on.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi i cant give a link as i dont have it up on the web, this is a project for college! But here is the code, hopefully this will help:
    <html>
    <title></title>
    <head></head>
    <body>
    <td BGCOLOR="#66ccff"><br><center>
    <FONT SIZE = 3 face = "ARIAL" COLOR = "#FFFFFF"><b>Step 2: Add student details</b><br><br>
    By Class&nbsp;&nbsp;


    <?php
    $link = mysql_connect("localhost","root") or die(mysql_error()); /* connect to db */
    mysql_select_db("teachers") or die(mysql_error()); /* select db to work from*/
    $query = "SELECT class_name FROM classes"; /* set query */
    $result = mysql_query($query) or die(mysql_error()); /* run query */

    if($result){ /* if sucessful */
    if(mysql_num_rows($result) == "0"){ /* check for no information */
    echo "No information in database table!";
    }

    else{ /* if information is present in table, proceed to make the form */

    echo '<select size="1" name="menu">'; /* echo outside of the while() because we don't want a lot of different drop down boxes! */

    while($row = mysql_fetch_assoc($result)){
    echo '<option>'.$row['option'].'</option>'; /* each individual result will make an option */
    }
    echo '</select>'; /* end the select box */
    echo '</form>';
    }
    }
    ?>


    <form method="post" action="register.php">
    <input type="submit" name="button_submit" value = "SELECT">

    </td>
    </form>
    </body>
    </html>

    Thanks,
    Gismo

  • #6
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i got it working, thanks for all your help!

  • #7
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glad to help
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #8
    New Coder
    Join Date
    Mar 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    No submit button

    Is there any way to have the items in the drop down menu then post to a new php when you select an option? Instead of having a submit button.

    I want the drop down menu to query the table and get the first column like it does in the above script. Then I want the user to select one and use that option in my next query on a new php but without the submit button.

    Thanks
    fyogi1

  • #9
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    <form method="post" name="form">
    <select onchange="form.submit()">
    [..options..]
    </select>
    </form>

  • #10
    New Coder
    Join Date
    Mar 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks Kurashu, you helped

    I think you forgot to and the action attribute. I got it to work like this:

    <form method="POST" action="test.php">
    <select name="name" onchange="form.submit()">
    [..options w/ value attribute....]
    </select>
    </form>

    Frank

  • #11
    New Coder
    Join Date
    Mar 2005
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have another question:

    what if i want to to call a certain file based on what i selected, i.e. i select from the drop down the word "apple" and it opens up a page called "apple_site.html" or more general: i choose word "word" and it opens up the file associated with the word "word". is that possible with a sql database and php?

  • #12
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Two ways. One: PHP. You make all of the options go to a page, get the value of the select menu, and redirect to the correct page through header('Location: WhatEver.html')

    Two: Javascript, make an onChange function that gets the selected value of the drop down menu, and changes the form action acordingly.

    The PHP way is far better. Universal, and easy.
    If I'm postin here, I NEED YOUR HELP!!

  • #13
    New Coder
    Join Date
    Mar 2005
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks.

    but how do i get the selected value with php exactely?

  • #14
    Regular Coder
    Join Date
    Jul 2004
    Location
    mile high city
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Get the value of the selected option from the POST array and redirect to the appropriate page.

    The form:
    Code:
    <form id="nav" method="post" action="nav.php">
      <div>
        <label for="nav_select">Go to:</label>
        <select id="nav_select" name="nav_select">
          <option value="http://yoursite.com/page1.php">Page 1</option>
          <option value="http://yoursite.com/page2.php">Page 2</option>
          <option value="http://yoursite.com/page3.php">Page 3</option>
        </select>
        <input type="submit" value="Go" id="go" />
      </div>
    </form>
    The form handler [referenced as nav.php above]:
    PHP Code:
    <?
    $redirect_to 
    $_POST['nav_select'] ;

    header"Location: $redirect_to" );
    exit ;
    ?>
    Computer, kill Flanders... Did I hear my name? My ears are burning...
    Good start. Now finish the job.

  • #15
    New Coder
    Join Date
    Mar 2005
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    thanks a lot, that's great!

    now i have another question

    in your example: if i want that page1.php displays a certain image based on what i selected before in the drop down –*how do i do that?

    background: in my project: i genereate a drop down from a database and then when i select its value, i.e. california it should go directly to a page where i see an image named california.jpg

    so with the solution you told me: the page1.php is the page california.php. but since i have 200 of such pages i don't want to edit 200 html files to change the image all the time. so it should generate my new california page only based on the value from the drop down.

    maybe it's a bit unclear but just ask me if you don't know how i mean it


  •  

    Posting Permissions

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