Solved

Multi-select select list - process in php

Posted on 2014-01-21
4
1,177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 110

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 110

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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