Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

multiple select box - result saved as 1 string

Links for php scripts

Moderators: macek, egami, gesf

multiple select box - result saved as 1 string

Postby b43 » Thu Jun 16, 2011 1:22 am

Hey there,

I've got a problem... I used this script to get the result of multiple select boxes
http://www.onlinetools.org/tricks/using ... select.php

Code: Select all
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
   <option value="one">one</option>
   <option value="two">two</option>
   <option value="three">three</option>
   <option value="four">four</option>
   <option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>

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


Now I want to save the result as just one string. E.g. I select one and four the string should be "one four" to be saved in the database.

Can anybody help me out?
b43
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Jun 16, 2011 1:15 am

Re: multiple select box - result saved as 1 string

Postby Nullsig » Thu Jun 16, 2011 3:53 am

change this:
Code: Select all
<?php
   $test=$_POST['test'];
   if ($test){
    foreach ($test as $t){echo 'You selected ',$t,'<br />';}
   }
?>


to this:
Code: Select all
<?php
   $test=(isset($_POST['test'])) ? $_POST['test'] : array();
//if something wasn't selected it still initializes an empty array

   $output = explode(" ",$test); //separate all elements into a space delimited string
?>



Now you just need to write the query that will insert that value to a database table.
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: multiple select box - result saved as 1 string

Postby b43 » Fri Jun 17, 2011 12:58 am

Thanks, that's what I was looking for.
Only problem is:

echo $output; --> Array

print_r($output); --> Array ( [0] => Array )

How can I output the result?

edit:
My current solution:
$outp = $test[0]." "$test[1]." ".$test[2];

this is working
b43
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Jun 16, 2011 1:15 am

Re: multiple select box - result saved as 1 string

Postby Nullsig » Fri Jun 17, 2011 4:24 am

So funny story, I always mix up which function does which....

The correct code is actually:
Code: Select all
<?php
   $test=(isset($_POST['test'])) ? $_POST['test'] : array();
//if something wasn't selected it still initializes an empty array

   $output = implode(" ",$test); //separate all elements into a space delimited string
?>
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.