Solved

Multi-select select list - process in php

Posted on 2014-01-21
4
1,107 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 108

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 82

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 82

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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now