How to Get Multiple Selected Values of the Select Box in PHP
Learn how to get multiple selected values of the selected box in PHP. In our snippet, we provide you with comprehensive information and handy examples.
The task of this tutorial is retrieving the multiple selected values of the selected box in PHP.
Go ahead to see the solution.
Using the Multiple Attribute of HTML
To meet the goal, you can use the multiple attribute of HTML.
Selecting multiple values of HTML relies on the browser and the operating system.
So, below we demonstrate the actions to take:
- On Windows, you should hold down the
<kbd class="highlighted">Ctrl</kbd>key to select multiple options. - On Mac, you should hold down the
<kbd class="highlighted">Command</kbd>key to select multiple options.
An example of creating a list of items with HTML looks like this:
php create a list of items with HTML
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form method="post" action="/form/submit">
<h4>SELECT SUBJECTS</h4>
<!--Using multiple to select multiple value-->
<select name="subject" multiple size="6">
<option value="english">ENGLISH</option>
<option value="maths">MATHS</option>
<option value="computer">COMPUTER</option>
<option value="chemistry">CHEMISTRY</option>
<option value="geography">GEOGRAPHY</option>
<option value="italian">ITALIAN</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>Now, let’s get to retrieving the multiple selected values from the list. Note: For best practices, PHP processing code should ideally be placed at the top of the file before any HTML output. You are required to use the form method and loop for getting the selected value:
php get the selected value
<?php
// PHP processing code should ideally be placed at the top of the file before any HTML output
$selectedSubjects = [];
if (isset($_POST["submit"])) {
if (isset($_POST["subject"])) {
$selectedSubjects = $_POST["subject"];
} else {
echo "Select an option first !!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form method="post" action="/form/submit">
<h4>SELECT SUBJECTS</h4>
<!-- The square brackets [] in name="subject[]" tell PHP to store the submitted values as an array -->
<select name="subject[]" multiple size="6">
<option value="english">ENGLISH</option>
<option value="maths">MATHS</option>
<option value="computer">COMPUTER</option>
<option value="chemistry">CHEMISTRY</option>
<option value="geography">GEOGRAPHY</option>
<option value="italian">ITALIAN</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
<?php if (!empty($selectedSubjects)): ?>
<h4>Your Selection:</h4>
<?php foreach ($selectedSubjects as $subject): ?>
<p>You selected: <?php echo htmlspecialchars($subject, ENT_QUOTES, 'UTF-8'); ?></p>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>So, in this snippet, we represented a handy method of getting multiple selected values of the selected box in PHP.