How to get selected option value in PHP

PHP

In this article, we will see how to get selected option values in PHP from HTML forms, with necessary examples.

What is "option value" in PHP?

Drop-down menus are created using the <select> and <option> tags. PHP allows users to select one or more options from a list through form submissions.

Syntax

Below is the syntax of the Option Value in PHP

<select name="select_list_name" size="n" multiple>  
  <option value="choice-name-1" selected>Text Label-1</option>  
  <option value="choice-name-2" selected>Text Label-2</option>  
</select>

The attributes related to the <select> tag are

  • Multiple: Allows selecting multiple choices from a list.
  • Name: Specifies the name of the drop-down list.
  • Size: Determines how many options are visible at once.

The attributes used with the <option> tag are

  • Value: Specifies the value sent when the form is submitted.
  • Selected: Pre-selects the option when the form loads.

Single Option Selection

Here is an example to show how to get a single selected option value in PHP ?

<!DOCTYPE html>  
<html lang="en">  
<head>  
  <meta charset="utf-8">  
  <title>PHP Dropdown Selection</title>  
  <style>  
    .wrapper {  
      max-width: 450px;  
      margin: 70px auto;  
      text-align: center;  
    }  
    select {  
      width: 100%;  
      height: 55px;  
      font-size: 16px;  
      padding: 10px;  
      border-radius: 5px;  
      border: 1px solid #ccc;  
    }  
    input[type="submit"] {  
      margin-top: 20px;  
      padding: 10px 30px;  
      background-color: #007bff;  
      color: white;  
      border: none;  
      border-radius: 5px;  
      cursor: pointer;  
    }  
  </style>  
</head>  
<body>  
  <div class="wrapper">  
    <h1>Movie Picker</h1>  
    <form action="" method="post">  
      <select name="movieList">  
        <option value="">Choose a movie</option>  
        <option value="inception">Inception</option>  
        <option value="titanic">Titanic</option>  
        <option value="interstellar">Interstellar</option>  
        <option value="gladiator">Gladiator</option>  
        <option value="avatar">Avatar</option>  
      </select>  
      <br>  
      <input type="submit" name="submitButton" value="Submit">  
    </form>  
    <?php  
      if (isset($_POST['submitButton'])) {  
        if (!empty($_POST['movieList'])) {  
          $chosenMovie = $_POST['movieList'];  
          echo '<p>You selected: <strong>' . ucfirst($chosenMovie) . '</strong></p>';  
        } else {  
          echo '<p style="color: red;">Please select a movie from the list.</p>';  
        }  
      }  
    ?>  
  </div>  
</body>  
</html>

This creates a dropdown menu allowing users to choose a movie. When submitted, PHP displays the selected movie name or shows an error if no selection was made.

Multiple Option Selection

Here's how to get multiple selected option values from a drop-down list ?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>PHP Multi-Select Dropdown</title>
  <style>
    .container {
      max-width: 500px;
      margin: 50px auto;
      text-align: center;
    }
    select {
      width: 100%;
      height: 150px;
      font-size: 14px;
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 10px;
    }
    input[type="submit"] {
      margin-top: 20px;
      padding: 10px 20px;
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Subject Selector</h1>
    <form method="post">
      <select name="subjectChoices[]" multiple size="6">
        <option value="data_structures">Data Structures</option>
        <option value="operating_systems">Operating Systems</option>
        <option value="physics">Physics</option>
        <option value="calculus">Calculus</option>
        <option value="ai">Artificial Intelligence</option>
        <option value="ml">Machine Learning</option>
      </select>
      <br>
      <input type="submit" name="submitButton" value="Submit">
    </form>
    <?php
      if (isset($_POST["submitButton"])) {
        if (isset($_POST["subjectChoices"])) {
          echo '<div>';
          foreach ($_POST['subjectChoices'] as $selectedSubject) {
            echo "You selected: " . ucfirst(str_replace("_", " ", $selectedSubject)) . "<br/>";
          }
          echo '</div>';
        } else {
          echo '<div style="color: red;">Please select at least one subject!</div>';
        }
      }
    ?>
  </div>
</body>
</html>

The key differences for multiple selection are

  • Add multiple attribute to the <select> tag
  • Use array notation name="fieldName[]" for the select field
  • Use foreach loop to process multiple selected values

Key Points

  • Use $_POST['fieldName'] to access single selected values
  • Use $_POST['fieldName[]'] with foreach for multiple selections
  • Always validate using isset() and !empty() before processing
  • The value attribute determines what data is sent to PHP, not the displayed text

Conclusion

Getting selected option values in PHP involves using the $_POST superglobal to access form data. For single selections, access values directly, while multiple selections require array handling with foreach loops.

Updated on: 2026-03-15T10:42:33+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements