PHP automatically converts name elements in forms to variables!
Examples:
#########################################
form element:
<input type="text" name="first_name" value="My Name">
PHP interpretation:
$first_name [color blue]=[/color][color teal]"My Name"[/color];
#########################################
form element:
Code:
<select name="age">
<option value="15">15</option>
<option value="25">25</option>
<option value="30" selected>30</option>
<option value="35">35</option>
</select>
PHP interpretation:
$age [color blue]=[/color][color teal]"30"[/color];
#########################################
form element:
a.)
<input type="checkbox" name="agree" value="YES">
b.)
<input type="checkbox" name="agree" value="YES" checked>
PHP interpretation:
a.)
$agree is not set
b.)
$agree [color blue]=[/color] [color teal]"YES"[/color];
*:->**:->**:->**:->**:->**:->**:->**:->*
[color red]NEW![/color]
*:->**:->**:->**:->**:->**:->**:->**:->*
#########################################
MULTIPLE SELECT FORM ELEMENTS
#########################################
Just recently I was asked to add to this FAQ a section about multiple select form elements, so here it is!
Sometime in your development career or hobby, you will come across the need to use multiple select form elements (multiple select dropdowns) where people can select more than one item from a dropdown.
We all know that PHP converts parameters to variables automatically. But what happens when you try to use a multiple select form element? Well, if you try to do the following:
Code:
<form action="/mylocation/script.php" method="get">
<select name="selection" multiple>
<option value="computer">computer</option>
<option value="monitor">monitor</option>
<option value="camera">camera</option>
<option vaue="printer">printer</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
and we chose
monitor and
printer then submitted our request, the PHP script script.php to test it:
Code:
<?php
while(list($key,$var) = each($HTTP_GET_VARS))
if(is_array($var)) {
for($a=0;$a<count($var);$a++)
echo $key." = ".$var[$a]."<br>\n";
}
else {
echo $key." = ".$var."<br>\n";
}
?>
would read:
product = printer
submit = submit
Why did it not recognize
monitor?
As we know, PHP automatically converts parameters to variables. Since selecting multiple items from one selection box, we are essentially creating an array. BUT, PHP does not know this and when we pass
selection=monitor&selection=printer&submit=submit, $selection is set to
monitor and then $selection is reset to
printer.
Here is how we correct this:
We have to inform PHP that we are creating an array so that each time an item is passed to
selection, it is really pushed into the end of the array. And, since PHP automatically converts parameters to variables, we can tell PHP that
selection is an array by changing
<select name="product" multiple>
to
<select name="product[]" multiple>
Now, when the variable is created it is actually $selection[] (which of course is an array. Now when we submit our request we will get:
product = monitor
product = printer
submit = submit
Remember that product is an array and you have to step through the array to access its elements (notice the section in script.php (our form parser) that has:
Code:
if(is_array($var)) {
for($a=0;$a<count($var);$a++)
echo $key." = ".$var[$a]."<br>\n";
}
If you did not do this, you would see
product = Array