How to get multiple selected values of select box in php?

Param-Ganak picture Param-Ganak · Mar 9, 2010 · Viewed 708k times · Source

I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:

I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.

Answer

Alex Jasmin picture Alex Jasmin · Mar 9, 2010

If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

Then you can acces the array in your PHP script

<?php
header("Content-Type: text/plain");

foreach ($_GET['select2'] as $selectedOption)
    echo $selectedOption."\n";

$_GET may be substituted by $_POST depending on the <form method="…" value.