0

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.

1
  • what are you trying to do? JOIN an array, or SPLIT a string? Commented Sep 5, 2012 at 5:39

8 Answers 8

3
<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...

Sign up to request clarification or add additional context in comments.

1 Comment

yes, It's working now, how insignificant I am. thank you sir. next stop, sql left join() function.
2

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;
}

Comments

2

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];

2 Comments

so i just can call it like this : $val=$_post['test1[0]'];?
Thank You Madame, now I just need to add the foreach loop to identify array values.
0

Implode is not getting any array. Its works on Array.

1 Comment

so, what function on getting array of values?
0

You are passing a string instead of an array. implode() turns an array into a string.

Comments

0

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.

Comments

0

Just look up this:

http://www.tizag.com/phpT/php-string-implode.php

And $ is used with variable.

Comments

0

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>

4 Comments

OMG, very straightforward.. Thank You Sibu.
@ThePowerintheRightHand i guess for getting selected values you don't have to follow such long procedure as others have mentioned :-)
I hope not, lol. my brain having a meltdown right now.. probably need to rest for awhile to avoid making simple mistakes.
@ThePowerintheRightHand just rest, it will come with practice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.