Current Location: Home> Latest Articles> How to Handle Dropdown List Options in PHP Forms

How to Handle Dropdown List Options in PHP Forms

M66 2025-06-12

How to Handle Dropdown List Options in PHP Forms

Dropdown lists are commonly used elements in web forms that allow users to select one or more values from predefined options. In PHP, we can process dropdown lists with simple code. This article will show you how to handle dropdown list options in a PHP form.

Dropdown List in HTML

Dropdown lists are usually defined using the tag defines the start and end of the dropdown list, while the


<form action="process_form.php" method="post">
  <label for="fruit">Please select your favorite fruit:</label>
  <select name="fruit" id="fruit">
    <option value="apple">Apple</option>
    <option value="banana">Banana</option>
    <option value="orange">Orange</option>
    <option value="grape">Grape</option>
  </select>
  <input type="submit" value="Submit">
</form>

In the above code, when the user selects an option and clicks the "Submit" button, the form will be submitted to the process_form.php file for processing.

Handling Dropdown List Selection in PHP

Now, let’s see how to handle this dropdown list in PHP. First, we need to get the value of the selected option. We can use the $_POST variable to retrieve the POST data submitted by the form.


$selectedFruit = $_POST['fruit'];
echo "You selected: " . $selectedFruit;

The above code will print out the fruit selected by the user. Here, $_POST['fruit'] refers to the value from the dropdown list with the name "fruit". The $selectedFruit variable will be assigned the corresponding value based on the user’s selection.

Setting a Default Selected Option

If you want to display a default selected option in the dropdown list, you can do so by adding the selected attribute to the


<option value="apple" selected>Apple</option>

This code will set "Apple" as the default selected option. Note that only one option can be set as the default.

Generating Dropdown List Options Dynamically

Additionally, you can dynamically generate the options in the dropdown list using a loop. This is especially useful if you are fetching the options from a database or other data sources. Here’s an example of generating options using a PHP loop:


$fruits = array("apple", "banana", "orange", "grape");

echo '<select name="fruit" id="fruit">';
foreach ($fruits as $fruit) {
  echo '<option value="' . $fruit . '">' . $fruit . '</option>';
}
echo '</select>';

The above code generates the options based on the values in the $fruits array. Each fruit’s value and display text will be set to the same value.

Conclusion

This article has demonstrated how to handle dropdown list options in PHP forms, including retrieving user selections, setting a default option, and dynamically generating options. With these basic techniques, you can efficiently handle dropdown lists in your forms. We hope this guide has helped you understand and work with dropdown lists in PHP!