Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Multi-select select list - process in php

Posted on 2014-01-21
4
Medium Priority
?
1,239 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 111

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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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 84

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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
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…
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.
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 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