SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Getting a value from a dropbox selection

    Hi there,

    I am working with a dropbox, trying to get a value from it depending the selection made, for going to another page and proceed according to the selection made.

    Dropbox elements are loaded from a MYSQL database, a simple query getting names. Almost all works fine, dropbox shows the element in database table, but nothing happens when I select one of them.
    Here the code I am using: the query and the form including the dropbox. I am trying to associate to every dropbox element an option value using $count, in order to be able to identify wich option was selected and proceed accordingly when jump to admin_menu.php. As stated before, I can select dropbox elements, but nothing happens.

    Any help would be greatly appreciated.
    Thanks a lot!!!


    <?php
    $SQL = "SELECT School_Name FROM Schools";
    $result = mysql_query($SQL, $db_handle) or die(mysql_error());
    ?>

    <form action="admin_menu.php" method="post">
    <select name = School>
    <?php
    $count = 0;
    while ($row = mysql_fetch_array($result)){
    echo "<option value='$count'>".$row['School_Name']."</option>";
    $count++;
    }
    ?>
    </select>
    </form>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You need to have a submit button for the form...
    PHP Code:
    <form action="admin_menu.php" method="post">
    <select name = School>
    <?php
    $count 
    0;
    while (
    $row mysql_fetch_array($result)){
    echo 
    "<option value='$count'>".$row['School_Name']."</option>";
    $count++;
    }
    ?>
    </select>
    <input type=submit>
    </form>
    OR, you need to submit the form using JS and onchange for that element.

    But, use a straight submit button for now.

    Also, check your table Schools - is there no other unique identifier which you can use to identify the School apart from the name?

    Otherwise you are going to have fun trying to fetch School number 12 when your backend does not know what 12 applies to:

    Most ppl start off with a table like this:
    Code:
    Schools
    ======
    id | int - autoincrement
    School_Name | varchar()
    eg
    Code:
    id | School_Name
    ==============
    12 | Big School
    ie
    PHP Code:
    <?php
    $SQL 
    "SELECT id, School_Name FROM Schools";
    $result mysql_query($SQL$db_handle) or die(mysql_error());
    ?>

    <form action="admin_menu.php" method="post">
    <select name = School>
    <?php
    while ($row mysql_fetch_array($result)){
    echo 
    "<option value='"$row['id'] ."'>"$row['School_Name'] ."</option>";
    }
    ?>
    </select>
    <input type=submit>
    </form>

    You do not have to have a numerical id in your table, its just that they generally do -- if you don't have one, and you cannot/do not want to change the table we can show you a way of selecting by School_Name but it will involve a bit more work.

    HTH

  3. #3
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi HTH,

    Thanks a lot for such a complete answer!!!
    About your question: yes, I do have tables with ID's, so I will be right now implementing the modifications for using them as you indicated.

    Thanks again!!!

  4. #4
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups,

    I implemented your suggestion. Now the dropbox is using the id's!!

    One last question please.
    When I press the button, website goes to ádmin_menu.php'. How can I get the dropbox selected id using a $_POST variable?
    Is it possible even when I have a button also?

    Thanks a lot!!!

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    As your form element for the dropdown is seemingly named School then you access the chosen ID by grabbing it from the POST varaiable which is incoming to your form handler "admin_menu.php";
    PHP Code:
    echo $_POST['School'] . ' is the chosen school id number'
    gotchas:

    You did not quote the name of the select element School, it will still work, but is generally frowned upon.
    Case matters! school !== School
    Your form must use the POST method for the above to work
    In future you can examine all the incoming vars by having this line at the top of your form handler.

    PHP Code:
    <?php
    // 2 lines of debug you remove when you go live
    var_dump($_POST)
    echo 
    '<hr />';


Bookmarks

Posting Permissions

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