How to Get HTML Multi-Select Values in PHP?

To get all selected values of a <select multiple> HTML element (when submitted via post for example, to a PHP script on the server-side), you need make sure the name attribute of the <select> element ends with square brackets ([]). When you do that, PHP is able to pick up the values as an array on the server-side, accessible via its superglobal arrays.

For example, suppose you have the following multi-select HTML element:

<form action="fav-music.php" method="post">
    <select multiple name="genres[]">
        <option value="rock" selected>Rock</option>
        <option value="pop" selected>Pop</option>
        <option value="country">Country</option>
        <option value="hip-hop">Hip-Hop</option>
        <!-- ... -->

    <input type="submit" />

When the form is submitted, you would read the selected values submitted to the server-side PHP script, for example, in the following way:

// fav-music.php

$values = $_POST['genres']; // e.g. ['Rock', 'Pop']

foreach ($values as $value) {
    echo $value;

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.