PHP: Request the values of a HTML form drop down list

前端 未结 2 1862
既然无缘
既然无缘 2021-01-07 13:37

I have a HTML form which is using a simple PHP mail() script to send its contents to an email address. I have just implemented a drop down list:



        
相关标签:
2条回答
  • 2021-01-07 14:24

    Use this for your html form:

    <select name="event">
     <option value="event1">Event 1</option>
     <option value="event2">Event 2</option>
     <option value="event3">Event 3</option>
    </select>
    

    And use this on your php page that your form submits to:

    $event = $_POST['event']
    

    or

    $event = $_GET['event']
    
    0 讨论(0)
  • 2021-01-07 14:25

    Each option element does not get its own name, just a value.

    Source: http://www.w3.org/TR/html401/interact/forms.html#h-17.6

    Remove the name attribute from your option elements:

    <select name="event">
      <option value="event1">Event 1</option>
      <option value="event2">Event 2</option>
      <option value="event3">Event 3</option>  
    </select>
    

    And depending on the method (post or get) your form element is using, you should use either:

    $event = $_POST["event"];
    

    or

    $event = $_GET["event"];
    

    Don't forget to use htmlspecialchars() or mysqli_real_escape_string() if saving to a database or printing back to the user in addition to using mail().

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