Getting data from a dropdown list

Discussion in 'PHP' started by enchance, Oct 16, 2007.

  1. #1
    How do you get data from a form which has a dropdown list? I have a list of books which my users could choose from (1 choice only), but I don't seem to be getting their selection.
     
    enchance, Oct 16, 2007 IP
  2. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use the POST type submission to gather variables from drop-down menus, such as $_POST['select_box_name_here'] when you process the form in PHP. Just make sure you specify values when making your options.
     
    audax, Oct 16, 2007 IP
  3. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I did. I use $val = $_POST['books'] but nothing comes out. I think the problem lies in the page itself since I ran Live HTTP Headers and found out that all the variable is sending is an "x" regardless of the selection. Ever got that before?
     
    enchance, Oct 16, 2007 IP
  4. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #4
    <form action="bookchoice.php" method="post">
    <select name="books" size="1">
    <option value="book1">Book 1</option>
    <option value="book2">Book 2</option>
    <option value="book3">Book 3</option>
    </select>
    <input type="submit" value "Submit" name="submit" />
    </form>
    
    HTML:
    bookchoice.php
    
    $books = $_POST['books'];
    echo "You have selected $books";
    
    PHP:
     
    YIAM, Oct 16, 2007 IP