?
Solved

PHP Multiple selection display

Posted on 2011-05-13
11
Medium Priority
?
238 Views
Last Modified: 2012-06-21
Hi Experts.

I'm unsure about the coding required to make a php file display multiple selections, so far I'm only getting one.

Here is my code:

HTML
                             <select name="sport" multiple="multiple" size="4">
                             <option value="soccer">Soccer</option>
                             <option value="cricket">Cricket</option>
                             <option value="squash">Squash</option>
                             <option value="golf">Golf</option>
                             <option value="tennis">Tennis</option>
                             <option value="basketball">Basketball</option>
                             <option value="baseball">Baseball</option>
                           </select>

Open in new window


PHP

<?php $sportstr = $_POST["sport"];?>
<strong>sport = </strong> <?php echo "$sportstr"; ?> <br />
 

Open in new window


I want to be able to select 4 different options shown in the HTML and those to be displayed when I submit the form.

Thanks in advance.

Rick
0
Comment
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35752543
You need to add name="sport[]"

take a look here:

<form action="#" method="post">
<select name="sport[]" multiple="multiple" size="4">
    <option value="soccer">Soccer</option>
    <option value="cricket">Cricket</option>
    <option value="squash">Squash</option>
    <option value="golf">Golf</option>
    <option value="tennis">Tennis</option>
    <option value="basketball">Basketball</option>
    <option value="baseball">Baseball</option>
</select>
<input type="submit" />

<?php 
    $sportstr = $_POST["sport"];
    print_r($_POST);
?>

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35752548
Here's a demo page for you.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Multi</title>
</head>
<body>
<h1>PHP Multi</h1>
<p>Select 4:</p>
<form action="" method="post">
<select name="sport[]" multiple="multiple" size="4">
<option value="soccer">Soccer</option>
<option value="cricket">Cricket</option>
<option value="squash">Squash</option>
<option value="golf">Golf</option>
<option value="tennis">Tennis</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
<?php 
//print_r($_POST);
if(isset($_POST["submit"])) {
if(isset($_POST["sport"])) {
$sportstr = $_POST["sport"];
echo "<strong>sports = </strong>";
foreach($sportstr as $value) {
	echo "$value&nbsp;";
	}
	}
else echo "No sports selected.";
echo "<br>";
} 
?> 
<a href="PHP-Multi.php">Again.</a>
</body>
</html>

Open in new window

0
 
LVL 8

Expert Comment

by:kivan24
ID: 35752560
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="sport[]" multiple="multiple" size="4">
         <option value="soccer">Soccer</option>
         <option value="cricket">Cricket</option>
         <option value="squash">Squash</option>
         <option value="golf">Golf</option>
         <option value="tennis">Tennis</option>
         <option value="basketball">Basketball</option>
         <option value="baseball">Baseball</option>
    </select>
<input type="submit" value="Send" />
</form>

<?php
	$sportstr=$_POST['sport'];
	if ($sportstr){
	 foreach ($sportstr as $s){echo 'You selected ',$s,'<br />';}
	}
?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35752561
Or - to be complete:

<form action="#" method="post">
<select name="sport[]" multiple="multiple" size="4">
    <option value="soccer">Soccer</option>
    <option value="cricket">Cricket</option>
    <option value="squash">Squash</option>
    <option value="golf">Golf</option>
    <option value="tennis">Tennis</option>
    <option value="basketball">Basketball</option>
    <option value="baseball">Baseball</option>
</select>
<input type="submit" />
<br /><br />

<?php 
    if(!empty($_POST)){
        $sportstr = $_POST['sport'];
        foreach($_POST['sport'] as $val)
            echo"<strong>sport = </strong>".$val."<br />";
    }
    
?>

Open in new window

0
 

Author Comment

by:Ronniel Allan Castanito
ID: 35752574
Thanks for the fast reply. I've tried your code but I'm still getting some problems. I'm going to assume I did something wrong in the rest of the code.

This is the output that I get.


Array ( [uname] => Test [mail] => test [addrs] => 123test [gender] => male [sport] => squash [mlist] => true )
the following information was received from the user:

name = Test
email = test
addrs = 123test
gender = male
sport = squash
mlist = true

I'll post all my code below.

HTML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 3 Task 1</title>
</head>
<body>
  <h1>PHP form demo</h1>
  <form id="userinfo" action="test.php" method="post">
  <p>Please fill in the following form</p>
  <p>Name  <input type="text" name="uname"/><br/>
     Email <input type="text" name="mail"/></p>
  
  <p>Address<br/>
  <textarea rows="5" cols="30" name="addrs"></textarea><br/>
  </p>
    
  <p>Your gender please:<br/>
  <input type="radio" name="gender" value="female"/> Female<br/>
  <input type="radio" name="gender" value="male" /> Male</p>
  <p>Your favourite sport: <select name="sport[]" multiple="multiple" size="4">
                             <option value="soccer">Soccer</option>
                             <option value="cricket">Cricket</option>
                             <option value="squash">Squash</option>
                             <option value="golf">Golf</option>
                             <option value="tennis">Tennis</option>
                             <option value="basketball">Basketball</option>
                             <option value="baseball">Baseball</option>
                           </select>
  </p>
  <p><input type="checkbox" name="mlist" value="true" />Please add me to the mailing list</p>
  <p><input type="submit" value="submit"/>&nbsp;
     <input type="reset"  value="reset"/></p>
  </form>
</body>
</html>

Open in new window


PHP

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP test</title>
</head>
<body>
<?php $namestr = $_POST["uname"];
$emailstr = $_POST["mail"]; 
$addrsstr = $_POST["addrs"];
$genderstr = $_POST["gender"];
    $sportstr = $_POST["sport"];
    print_r($_POST);
$mliststr = $_POST["mlist"];?>
<p>the following information was received from the user:</p>
<p><strong>name = </strong> <?php echo "$namestr"; ?><br/>
<strong>email = </strong> <?php echo "$emailstr"; ?><br />
<strong>addrs = </strong> <?php echo "$addrsstr"; ?><br />
<strong>gender = </strong> <?php echo "$genderstr"; ?><br />
<strong>sport = </strong> <?php echo "$sportstr"; ?> <br />
<strong>mlist = </strong> <?php echo "$mliststr"; ?></p><br />

</body>
</html>

Open in new window


Rick.

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35752671
Try this as PHP file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP test</title>
</head>
<body>
<?php $namestr = $_POST["uname"];
$emailstr = $_POST["mail"]; 
$addrsstr = $_POST["addrs"];
$genderstr = $_POST["gender"];
    $sportstr = $_POST["sport"];
    print_r($_POST);
$mliststr = $_POST["mlist"];?>
<p>the following information was received from the user:</p>
<p><strong>name = </strong> <?php echo "$namestr"; ?><br/>
<strong>email = </strong> <?php echo "$emailstr"; ?><br />
<strong>addrs = </strong> <?php echo "$addrsstr"; ?><br />
<strong>gender = </strong> <?php echo "$genderstr"; ?><br />
<?php
        foreach($_POST['sport'] as $sportstr)
            echo"<strong>sport = </strong>".$sportstr."<br />";
?>
<strong>mlist = </strong> <?php echo "$mliststr"; ?></p><br />

</body>
</html>    

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35752674
Sorry, line 22 - 23

        foreach($sportstr as $val)
            echo"<strong>sport = </strong>".$val."<br />";
0
 
LVL 8

Expert Comment

by:kivan24
ID: 35752683
in PHP line:

replace line #13
$sportstr = implode(', ', $_POST["sport"]);

Open in new window

delete line #14.
0
 

Author Comment

by:Ronniel Allan Castanito
ID: 35752706
I've tried both solutions.

I get the following for both:


Array ( [uname] => test [mail] => test [addrs] => test123 [gender] => male [sport] => squash [mlist] => true )
the following information was received from the user:

name = test
email = test
addrs = test123
gender = male
mlist = true


PHP Warning: Invalid argument supplied for foreach() on line 22

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 1000 total points
ID: 35752725
This works - are you sure you're selecting the values properly. It seems that you selected only one value. You need to ctrl+click to select two or more

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP test</title>
</head>
<body>
<?php $namestr = $_POST["uname"];
$emailstr = $_POST["mail"]; 
$addrsstr = $_POST["addrs"];
$genderstr = $_POST["gender"];
$sportstr = $_POST["sport"];
$mliststr = $_POST["mlist"];?>
<p>the following information was received from the user:</p>
<p><strong>name = </strong> <?php echo "$namestr"; ?><br/>
<strong>email = </strong> <?php echo "$emailstr"; ?><br />
<strong>addrs = </strong> <?php echo "$addrsstr"; ?><br />
<strong>gender = </strong> <?php echo "$genderstr"; ?><br />
<?php
    foreach($sportstr as $val)
        echo"<strong>sport = </strong>".$val."<br />";
?>
<strong>mlist = </strong> <?php echo "$mliststr"; ?></p><br />

</body>
</html>

Open in new window

0
 
LVL 8

Assisted Solution

by:kivan24
kivan24 earned 1000 total points
ID: 35752735
Try my :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

840 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