How to get the values of an HTML <select> tag using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kosaks
    New Member
    • May 2010
    • 10

    How to get the values of an HTML <select> tag using PHP

    Hello to everyone!

    I would like to ask if how can i get the selected value of an HTML <select> tag using PHP. Im new to PHP and MYSQL programming. This is my code.

    [HTML]

    <form method="POST" name="me">
    <select size="1" name="DropDownB ox">
    <option value="99">Defa ult</option>
    <option value="1">Choic e 1</option>
    <option value="2">Choic e 2</option>
    <option value="3">Choic e 3</option>
    <option value="4">Choic e 4</option>
    </select><input type="submit" value="Submit" name="BTN"/></p>
    </form>

    [/HTML]

    [PHP]

    if(isset($_POST['BTN'])){
    $value = $_POST['DropDownBox'];
    $con = mysql_connect(" localhost","sam ple","test");
    mysql_selectdb( "sampleDB",$con );
    $SQL = "INSERT INTO selectedChoices (`choices`) VALUES ('$value');";
    mysql_query($SQ L, $con);
    }
    $mysql_close($c on);

    [/PHP]

    The data is saved to the database but im wondering because every time i save a new record the value in the database is the first value in my <select> tag which is the "Default" value. I don't know why php can't get the selected value on my <select> tag. Can someone correct me with my errors.

    Any help will be appreciated!

    Thanks in advance

    Jeff
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Probably because you haven't specified to where you are posting eg
    Code:
    <form method="POST" name="me"
    action="path/to/my.php">

    Comment

    Working...