what “type” is an option chosen from a select tag

前端 未结 3 1586
孤街浪徒
孤街浪徒 2021-01-25 02:21

I am new to PHP and attempting get the value of an option chosen from a select tag. I am familiar with some types of input such as radio, text, checkboxes e.t.c. my question is

相关标签:
3条回答
  • 2021-01-25 02:43

    You need to give the name attribute to the select, not the options.

    <form name="chosen_party" action="party.php" method="get">
        <select name="party_type">
            <option value="democrat">Democrat</option>
            <option value="republican">Republican</option>
            <option value="independent">Independent</option>
            <option value="undecided">Undecided</option> 
        </select>
        <input type="submit" value="Submit">
    </form>
    

    Then in php, you can access $_GET['party_type'], which will be the option that was chosen.

    0 讨论(0)
  • 2021-01-25 02:58

    First give a name to select tag. and in the case of option give a value to each option. like this..

    <form name="chosen_party" action=party.php method="get">
    <select name="party_type">
    <option   value="democrat">Democrat</option>
    <option   value="republican">Republican</option>
    <option   value="independent">Independent</option>
    <option   value="undecided">Undecided</option> 
    </select>
    <input type=submit value="Submit">
    </form>
    

    Then in the php there is a little syntax problem. You have to put $party_select outside of the quote. The code should be..

    <?php
    $party_select = $_GET['party_type'];
    echo 'User has chosen '.$party_select;
    ?>
    

    Hope this helps..

    0 讨论(0)
  • 2021-01-25 03:02

    option says that it is an option. Don't need the type attribute at all.

    <select name="myOptionList">
      <option value="1">First option</option>
      <option value="2">Second option</option>
    </select>
    

    this is enough markup, more here http://www.w3schools.com/tags/tag_select.asp

    0 讨论(0)
提交回复
热议问题