note 87505 deleted from reserved.variables.get by thiago
| From: | thiago@php.net | Date: | Mon, 08 Dec 2008 12:04:04 +0000 |
| Subject: | note 87505 deleted from reserved.variables.get by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147916@lists.php.net to get a copy of this message | ||
Note Submitter: chatchai_chingchit at hotmail dot com
----
Note: It is possible that you may have a set of input elements with the same name, like a
multiple-select box or some checkboxes, as shown in code below.
<?php
if(isset($_GET["name"])){
var_dump($_GET);
}
?>
<html>
<body>
<form name="test_form" method="get">
Name:<br/>
<input type="text" name="name"/><br/>
Multiple Selection:<br/>
<select name="select" multiple="true">
<option value="s1">s1</option>
<option value="s2">s2</option>
<option value="s3">s3</option>
</select><br/>
Checkboxes:<br/>
<input type="checkbox" name="box" value="b1"/>Box 1<br/>
<input type="checkbox" name="box" value="b2"/>Box 2<br/>
<input type="checkbox" name="box" value="b3"/>Box 3<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
For example, if I have selected "s1", "s2" and "s3" in the selection
box and checked all the checkboxes, then I press submit button. The "Get" method gives me
a query string like this:
name=filledname&select=s1&select=s2&select=s3&box=b1&box=b2&box=b3
And what the "var_dump" function gives me is:
array(3) { ["name"]=> string(10) "filledname" ["select"]=>
string(2) "s3" ["box"]=> string(2) "b3" }
Can you see? The reciever won't know that I have selected all data of the selection box and
checkboxes. I will demonstrate some processes from the situation:
1. My $_GET["select"] is "s1", because the first key-value pair says that
"select=s1".
2. My $_GET["select"] is changed to "s2", because another key-value pair says
that "select=s2".
3. My $_GET["select"] is changed to "s3", because the last key-value pair says
that "select=s3".
So my $_GET["select"] is finally "s3" as shown in the "var_dump"
function's output. And so do $_GET["box"]. I have found that the situation is also
happened with $_POST.
Now my solution is using javascript help mixing the values ("s1", "s2",
"s3") in one hidden input element ("s1:s2:s3"), then send the string to receiver
which will process it (split it and get the values).