Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending multiple select values in a $_GET string. Is it possible?

Status
Not open for further replies.

databasis

Programmer
Feb 18, 2002
23
GB
I have a form with dynamically created drop-down lists. I send ALL list 'states' back to the same page with a $_GET string so that the drop-down boxes still hold the user's selections as they continue to select from the remaining lists on the form. The $_POST action ONLY happens after all lists are selected.

However, one of the lists now needs to allow users to select more than one option.

The value of the selection box is now an array and while this is easy to process if the form action is POST, like so...

Code:
	$PHPArray = $_POST['formValue'];
	foreach($PHPArray as $value)
	{
		echo $value;
                ....do stuff with each value

	}
The same won't work if I use $_GET.

The $_GET string for example might look like this...
Code:
      [URL unfurl="true"]http://localhost[/URL] etc/test.php?formValue=2&formValue=3
but the following code won't work:
Code:
        $PHPArray = $_GET['formValue'];        
        foreach($PHPArray as $value)
	{
		echo $value;
                ....do stuff with each value

	}

Would anyone have an idea why?
I'd appreciate some help with this.

Maria
 
If you're using checkboxes or select tags that allow multiple inputs, you need to let PHP know to expect the multiple inputs.

If your HTML looks something like:

Code:
<form method=<whatever> action="foo.php>
   <select name="foo" multiple>
      .
      .
      .
   </select>
   <input type="submit">
<form>

If your user selects multiple inputs, only the last will be recognized by PHP. However, if you name your select tag a little differently:

Code:
<form method=<whatever> action="foo.php>
   <select name="foo[red][][/red]" multiple>
      .
      .
      .
   </select>
   <input type="submit">
<form>

Then $_GET['foo'] or $_POST['foo'] (as appropriate) will itself be an array containing all the values.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
It works fine, just look at your query string. But it won't be loaded into the $_GET array as you like.

You'll need to manually parse the $_SERVER['QUERY_STRING'] value.

-Rob
 
Ooops, sleipnir is correct, I didn't think that worked for selects in get strings, but it does.
 
Many thanks to you both! Rob, I investigated the $SERVER['QUERY_STRING'] value and hurrah! I see exactly what you mean! $_POST forms KNOW that the value is an array but $_GET forms haven't a clue. I didn't know that!

I appreciate your help very much.

Maria
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top