Tim's Web Site 2.0

About Me

Miscellaneous

Geek Stuff

Skydiving


My Amazon.com Wish List

Geek Stuff : Tutorials : PHP : Accessing Form Variables with PHP

Accessing form variables in PHP is not hard, but some form items can be a little confusing to newer developers (n00bs) when some elements use arrays like the checkbox and select fields that allow multiple selections.

HTML Form
To begin we will create an HTML form using a variety of different elements. You can click the "show form source code" link to view the HTML form source code.

Name:
Gender:
Programmer: Yes
No
Colors: Red
Blue
Green
Browsers: (shift-click to select multiple)
 
  show form source code

HTML Form Source Code  [ hide form source code]
<form name="sampleForm" method="post" action="accessing_form_variables2.php">
<table border="0">
   <tr>
      <td align="right">Name:</td>
      <td><input type="text" name="name" size="30" class="normal"></td>
   </tr>
   <tr>
      <td align="right">Gender:</td>
      <td><select name="gender">
      <option value="Male">Male</option>
      <option value="Female">Female</option>
      </select></td>
   </tr>
   <tr>
      <td align="right" valign="top">Programmer:</td>
      <td>
      <input type="radio" name="programmer" value="Yes"> Yes<br/>
      <input type="radio" name="programmer" value="No"> No</td>
   </tr>
   <tr>
      <td align="right" valign="top">Colors:</td>
      <td>
      <input type="checkbox" name="colors[]" value="Red"> Red<br/>
      <input type="checkbox" name="colors[]" value="Blue"> Blue<br/>
      <input type="checkbox" name="colors[]" value="Green"> Green</td>
   </tr>
   <tr>
      <td align="right" valign="top">Browsers:</td>
      <td><select name="browsers[]" multiple size="3">
      <option value="Firefox">Firefox</option>
      <option value="Safari">Safari</option>
      <option value="Internet Explorer">Internet Explorer</option>
      </select> (shift-click to select multiple)</td>
   </tr>
   <tr>
      <td></td>
      <td><input type="submit" value="Submit!"></td>
   </tr>
</table></form>

PHP Code : Single Value Elements
For most form elements (text, radio, and select in our example form) you can easily get the value of a field with very simple code. Form field/value pairs end up in PHP built-in global arrays, $_POST for POST method and $_GET for GET method submissions.

$_POST['name'] /* if using POST method */

$_GET['name'] /* if using GET method */

The POST method submits form field/value pairs through the HTTP headers, and the GET method submits field/value pairs through the actual URL. If using the GET method to submit your form you would see something like the following in the URL on the receiving page.

http://www.yourdomain.com/whatever.php?name=Tim%20Patterson

You can see the form field and it's value are separated by the = equal sign, and multiple field/value pairs would then be separated using the & ampersand sign. All of this would be appended to the receiving page URL after a ? question mark.

PHP Code : Multiple Value Elements
Some elements, such as checkbox and multiple select fields, can hold multiple values for the same form field. We access the values in these fields as Array values.

To designate that a particular form field can contain multiple values, and will be an "array", we need to append [] brackets to the end of the field name. Notice in the form source code above that the 'Colors' field name is colors[] and the 'Browsers' field name is browsers[].

We can list the values for something like our checkbox field with a while() loop.

echo "<u>Colors Picked:</u><br/>\n";
while(list($field, $value) = each($_POST['colors'])) {
   // display each color chosen as we loop through array
   echo $value . "<br/>\n";
}

We can also list the values for something like our checkbox field with a for() loop.

echo "<u>Colors Picked:</u><br/>\n";
for($counter = 0; $counter < count($_POST['colors']); $counter++) {
   // display each color chosen as we loop through array
   echo $_POST['colors'][$counter] . "<br/>\n";
}

If we chose checkboxes for 'Red' and 'Green' in our web form above either block of code above would then output something like:

Colors Picked:
Red
Green

How can we detect if a form field has multiple values?
On your form processing page you might want to be able to loop through all of the submitted form fields at once instead of processing them individually.

We can detect if a given form field value has multiple values (determine if it is an Array) with the is_array() function included in PHP. If we find a form field is an array we loop through it, otherwise we can just display the value if not. Some example code follows.

<table border="0">
<?php
// first loop through all submitted form fields
while(list($field, $value) = each($_POST)) {
   echo "<tr valign=\"top\">\n";
   echo "<td align=\"right\">" . $field . ":</td>\n";
   // check if form field is an array (like checkbox or multiple select)
   if(is_array($value)) {
      echo "<td>";
      // loop through form field array and display values
      while(list($arrayField, $arrayValue) = each($value)) {
         echo $arrayValue . "<br/>\n";
      }
      echo "</td>\n";
   // if form field is not an array (text, select, radio) just display
   } else {
      echo "<td>" . $value . "</td>\n";
   }
   echo "</tr>\n";
}
?>
</table>

This tutorial isn't complete or all inclusive, but should help get you started in processing form fields that submit multiple values in arrays. I hope my tutorial helps!

©1995 - 2008 Tim Patterson, All Rights Reserved (Unless otherwise noted)