Ricky Nguyen
asked on
PHP Multiple selection display
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
PHP
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
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>
PHP
<?php $sportstr = $_POST["sport"];?>
<strong>sport = </strong> <?php echo "$sportstr"; ?> <br />
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
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 ";
}
}
else echo "No sports selected.";
echo "<br>";
}
?>
<a href="PHP-Multi.php">Again.</a>
</body>
</html>
<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 />';}
}
?>
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 />";
}
?>
ASKER
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.
I'll post all my code below.
HTML
PHP
Rick.
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"/>
<input type="reset" value="reset"/></p>
</form>
</body>
</html>
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>
Rick.
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>
Sorry, line 22 - 23
foreach($sportstr as $val)
echo"<strong>sport = </strong>".$val."<br />";
foreach($sportstr as $val)
echo"<strong>sport = </strong>".$val."<br />";
in PHP line:
replace line #13
replace line #13
$sportstr = implode(', ', $_POST["sport"]);
delete line #14.
ASKER
I've tried both solutions.
I get the following for both:
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
take a look here:
Open in new window