Reputation: 15
I'm trying to make this implode function work.
This is the form part, assume all the item has been selected.
<form method="post">
<select name="test1" multiple="multiple" id="test1">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
</select>
</form>
The PHP part
<?php
$var1 = array();
$joinedString = array();
$var1 = $_POST['test1'];
$joinedString = implode(',', $var1);
?>
But The echoing part doesn't work, it gives me error, and only displaying only the first array value.
<?php
$echo $joinedString[0];
$echo $joinedString[1];
$echo $joinedString[2];
$echo $joinedString[3];
$echo $joinedString[4];
?>
Thank You guys, I'm quite new in programming. I always forgot that the code executed line by line, and always confused with variable and values, and yes, in real world I am also a clumsy & insignificant person.
Upvotes: 0
Views: 4008
Reputation:
Change
<select name="test1" multiple="multiple" id="test1">
to
<select name="test1[]" multiple="multiple" id="test1">
And it's already an array
$var1 = $_POST['test1'];
$imploded = implode(",", $var1);
echo $imploded;
//FOR GETTING INDIVIDUAL ITEMS FROM array
echo $var1[0];
Upvotes: 2
Reputation: 1075
<form method="post" action="sear.php">
<select name="test1[]" multiple="multiple" id="test1">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
<input type="submit" name="submit" value="submit" />
</select>
</form>
<?php
$var1 = array();
$joinedString = array();
$var1 = $_POST['test1'];
$joinedString = implode(',', $var1);
echo $joinedString;
?>
After getting the post values it definitely works.... Try it...
Upvotes: 3
Reputation: 4617
You are trying to use implode your array using (,) but post array values does not contain values seperated by comma, therefore you will have to use foreach.
<?php
$var1 = array();
$joinedString = array();
$var1 = $_POST['test1'];
foreach($var1 as $values)
echo $values."<br/>";
?>
<form method="post">
<select name="test1[]" multiple="multiple" id="test1">
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
<option value="5">item5</option>
</select>
<input type="submit" >
</form>
Upvotes: 0
Reputation: 2594
Use
<select name="test1[]" multiple="multiple" id="test1">
In php file.
$var1 = isset($_POST['test1']) ? $_POST['test1']: 0 ;
print_r($var1); //gives array
foreach($var1 as $var) {
echo $var;
}
Upvotes: 2
Reputation: 3267
Just look up this:
http://www.tizag.com/phpT/php-string-implode.php
And $
is used with variable.
Upvotes: 0
Reputation: 2042
In your script, $_POST['test1'] will only contain the submitted value of the select box, not the entire set of values. Since $var1 contains only a string, implode() will error out.
Upvotes: 0
Reputation: 490253
You are passing a string instead of an array. implode()
turns an array into a string.
Upvotes: 0