using implode to combine multiple select
I have a php page like this :
<html>
<body>
<form method="post" action="catch_combo.php">
<select name="selr[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="submit">
</form>
</body>
</html>
I am trying to catch the sele开发者_开发技巧cted values in catch_combo.php which looks like this:
<?php
$two;
if(isset($_REQUEST['selr']))
{
$one=$_POST['selr'];
foreach ($one as $a)
{
$two = implode(",", $a);
}
echo $two;
}
?>
When I run this it says
'Invalid arguments passed for implode' I am missing something?
$two = '';
if(isset($_REQUEST['selr']))
{
$one=$_POST['selr'];
foreach ($one as $a=>$value)
{
$two .= ', '.$value;
}
echo $two;
}
No need for implode.
but easier way is:
$two = implode(',', $_POST['selr']);
You're not passing an array to implode()
, that's why it complains. Try this:
if (isset($_REQUEST['selr'])) {
echo implode(',', $_REQUEST['selr']);
}
The question was about invalid arguments passed for implode()
The 2nd argument for implode()
must be an array.
$a in your sample code is not an array.
精彩评论