Solved

Multi-select select list - process in php

Posted on 2014-01-21
4
1,140 Views
Last Modified: 2014-02-06
If you have a form with a select element with multiple selections allowed, how do you process the selections in the php form processing program?

If the form element is called "choices", in the php program how to I separate out the individual choices made?

Thanks
0
Comment
Question by:Richard Korts
  • 2
  • 2
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39798826
Please post the form so we can show you a tested-and-working code example, thanks.  

In the action script you can learn about what the form sends by using var_dump($_REQUEST)
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39798907
Multi-select came up in another question but I never got around to making a demo page.  This time I did and it took more work than I thought.  Save the code below as 'multiselect.php' and run it.  For 'multiple select' to work, you must (for PHP) declare the name as an array with '[]'.  I think you may also need enctype="multipart/form-data" in there too.  I'm going to try a second version that uses 'GET' instead of 'POST'.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Multi-select select list in php</title>
</head>
<body>
<h1>Multi-select select list in php</h1>

<form action="" method="post" enctype="multipart/form-data">

<select name="sometext[]" id="sometext" multiple="multiple">
<option value="1">text1</option>
<option value="2">text2</option>
<option value="3">text3</option>
<option value="4">text4</option>
<option value="5">text5</option>
<option value="6">text6</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
<pre>

<?php 
if(isset($_REQUEST['submit'])) {
	echo "<b>GET</b><br>";
	var_dump($_GET);
	echo "<br><b>POST</b><br>";
	var_dump($_POST);
	echo "<br><b>REQUEST</b><br>";
	var_dump($_REQUEST);
	}
?>

</pre>
<br /> 
<a href="multiselect.php">multiselect.php</a>
</body>
</html>

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39798911
A 'GET' version works as well.  Save this one as 'multiselectget.php'.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Multi-select select list in php</title>
</head>
<body>
<h1>Multi-select select list in php</h1>

<form action="" method="post" enctype="multipart/form-data">

<select name="sometext[]" id="sometext" multiple="multiple">
<option value="1">text1</option>
<option value="2">text2</option>
<option value="3">text3</option>
<option value="4">text4</option>
<option value="5">text5</option>
<option value="6">text6</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
<pre>

<?php 
if(isset($_REQUEST['submit'])) {
	echo "<b>GET</b><br>";
	var_dump($_GET);
	echo "<br><b>POST</b><br>";
	var_dump($_POST);
	echo "<br><b>REQUEST</b><br>";
	var_dump($_REQUEST);
	}
?>

</pre>
<br /> 
<a href="multiselectget.php">multiselectget.php</a>
</body>
</html>

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39799776
Dave: You are correct about the bracket declaration.  If you want to send more than one data element in a single request variable, you need to tell it to create an array by including the [] notation.  You can put a value in the brackets, as long as it is unique.  If you omit the brackets, or put in a non-unique value you will get the last-in-list of the selected / named elements because PHP will overwrite the data for identically named keys as it prepares the request variables ($_POST and $_GET).

When the data is received in the PHP script, the request variable will be an array that contains another array.  For the example above, you would find the data elements with something like this:

foreach ($_POST['sometext'] as $value) {
    echo $value;
}

Open in new window

Pretty sure you don't need enctype form attribute for multi-selects, but it does not hurt to have it there.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question