Home >Backend Development >PHP Tutorial >How to get selected option value in PHP

How to get selected option value in PHP

Susan Sarandon
Susan SarandonOriginal
2025-02-07 11:33:11642browse

This article demonstrates how to retrieve selected option values from HTML forms using PHP. We'll cover single and multiple selection scenarios with illustrative examples.

What are "option values" in PHP?

In the context of PHP and HTML forms, "option values" refer to the data associated with each item in a dropdown menu ( <select></select> element). When a user selects an option, its corresponding value is submitted to the server, allowing your PHP script to process the user's choice.

Syntax

Dropdown menus are created using the HTML <select></select> and <option></option> tags. The key attributes are:

  • <select></select>:

    • multiple: Allows users to select multiple options.
    • name: Specifies the name of the dropdown, used to identify it in the PHP script.
  • <option></option>:

    • value: The value submitted when the option is selected.
    • selected: Pre-selects an option when the form loads.

Example 1: Single Selection

This example shows how to retrieve a single selected option value.

<code class="language-php"><!DOCTYPE html>


<title>PHP Dropdown Selection</title>
<style>
/* CSS styles (omitted for brevity) */
</style>


  <h1>Select a Movie</h1>
  <div class="wrapper">
    <form method="post" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>">
      <div class="dropdown-container">
        <select name="movie">
          <option value="">Select a movie</option>
          <option value="Movie A">Movie A</option>
          <option value="Movie B">Movie B</option>
        </select>
      </div>
      <input type="submit" value="Submit">
    </form>
    <?php
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["movie"])) {
          echo "<p class='message error'>Please select a movie.</p>";
        } else {
          echo "<p>You selected: " . $_POST["movie"] . "</p>";
        }
      }
    ?>
  </div>

</code>

How to get selected option value in PHP

This code creates a simple form with a dropdown. The PHP code checks if a movie was selected and displays the selection or an error message accordingly.

Example 2: Multiple Selection

This example demonstrates how to handle multiple selections.

<code class="language-php"><!DOCTYPE html>


<title>PHP Multi-Select Dropdown</title>
<style>
/* CSS styles (omitted for brevity) */
</style>


  <h1>Select Subjects</h1>
  <div class="container">
    <form method="post" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>">
      <div class="dropdown-container">
        <select name="subjects[]" multiple>
          <option value="Math">Math</option>
          <option value="Science">Science</option>
          <option value="English">English</option>
        </select>
      </div>
      <input type="submit" value="Submit">
    </form>
    <?php
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["subjects"])) {
          echo "<p class='message error'>Please select at least one subject.</p>";
        } else {
          echo "<p>You selected: ";
          foreach($_POST["subjects"] as $subject) {
            echo $subject . ", ";
          }
          echo "</p>";
        }
      }
    ?>
  </div>

</code>

How to get selected option value in PHP

Note the [] in name="subjects[]" which allows for multiple selections. The PHP code iterates through the selected subjects and displays them. Error handling is included for the case where no subject is chosen. Remember to replace placeholder CSS with your own styling.

The above is the detailed content of How to get selected option value in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn