Forum Moderators: coopster

Message Too Old, No Replies

GET multiple variables of same name

Is this possible?

         

yowza

9:57 pm on Apr 19, 2004 (gmt 0)

10+ Year Member



Is it possible to GET multiple variables with the same identifier from a URL and combine them into an array.

I am a newbie, so maybe there is a better way to do this. I have a form with a <select multiple> box on it. If somebody selects more than one of these I want to combine them, place a comma between them, and store them in a database.

How can this be done?

Thanks

Birdman

10:09 pm on Apr 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do it by adding brackets to the name attribute in the select input. Then loop them in the script:

<select name="your_form_field[]" multiple="multiple">

$count = count($_POST["your_form_field"]);

for ($i=0; $i<$count; $i++){
print $_POST["your_form_field"][$i];
}

I think I got that right?

yowza

10:44 pm on Apr 19, 2004 (gmt 0)

10+ Year Member



Yeah, that worked great. Thanks!

I just found out that I can't get the array out of the database without unserializing it first. I guess I have some digging to do.

yowza

8:43 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



Is it possible to combine them into a string instead of into an array?

Netizen

8:56 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



If you have

<select name="your_form_field[]" multiple="multiple">

etc, then to make a string to put in the database just do

$string = implode(',',$_POST['your_form_field']);

This will join all the entries in $_POST['your_form_field'] separated by a comma.

yowza

9:16 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



Netizen,

Thanks, but that is not working. It is not entering anything into the database.

Netizen

12:35 pm on Apr 21, 2004 (gmt 0)

10+ Year Member



Are the POSTed vars there i.e. if you do

foreach ($_POST['your_form_field'] as $selectedValue) {
print "$selectedValue<br>\n";
}

do you get anything?

yowza

5:36 pm on Apr 21, 2004 (gmt 0)

10+ Year Member



Ok, now it stores it in the database, my mistake. However, when it is stored in the database it says "Array". I think that the implode() function makes it an array, right? Is there a way to keep it as a string?

Netizen

8:04 pm on Apr 21, 2004 (gmt 0)

10+ Year Member



Could you post a snippet of the relevant code - it might help.

yowza

4:05 pm on Apr 22, 2004 (gmt 0)

10+ Year Member



Thanks for all your help.

I decided to do it another way.

I would like to figure it out someday, but I have a deadline to meet.