PHP form handling

A simple form

Within PHP a special array called $_POST is kept that holds all the data that is sent from a form (except for file fields – see below). Each entry in the array is a (key, value) pair of the form (field name, posted data). There is also an array for data sent via the get method ($_GET). The simple form below contains various fields each with the name and id attribute.

<form name="form1" id="form1" method="post" action="myform.php">
   <input name="textbox" id="textbox" type="text" value="Enter text" /><br />
   <textarea name="textarea" id="textarea" cols="30" rows="5">Text</textarea><br />
   <input type="hidden" name="hiddenfield1" id="hiddenfield1" value="hiddenfieldvalue" />
   <label><input type="checkbox" name="checkbox1" id="checkbox1" />checkbox1</label><br />
   <label><input type="checkbox" name="checkbox2" id="checkbox2" />checkbox2</label><br />
   <label><input type="checkbox" name="checkbox3" id="checkbox3" />checkbox3</label><br />
   <label><input type="checkbox" name="checkbox4" id="checkbox4" />checkbox4</label><br />
   <label><input type="radio" name="Radio1" value="Radiobutton1" id="Radio1_0" />Radiobutton1</label><br />
   <label><input type="radio" name="Radio1" value="Radiobutton2" id="Radio1_1" />Radiobutton2</label><br />
   <label><input type="radio" name="Radio1" value="Radiobutton3" id="Radio1_2" />Radiobutton3</label><br />
   <select name="select" id="select">
      <option value="menuitem1">menuitem1</option>
      <option value="menuitem2">menuitem2</option>
      <option value="menuitem3">menuitem3</option>
   </select>
   <input type="submit" name="submitbutton" id="submitbutton" value="Submit" />
</form>

The two important attributes in the form element are the method and the action. The method describes how the data will be sent to the script within the action attribute. The two options are post and get. When the data is ‘posted’ it is sent directly to the script but when it is set to get the data is attached to the query string. Below is the output when the form above is posted.

//print_r($_POST);
Array
(
      [textbox] => Enter text
      [textarea] => Text
      [hiddenfield1] => hiddenfieldvalue
      [checkbox3] => on
      [checkbox4] => on
      [Radio1] => radiobutton3
      [select] => menuitem2
      [submitbutton] => Submit
)

The variable is a map with the key being the name of the form element and the value being the posted data for the appropriate field. This data shows that the third radio button was selected and both checkboxes 3 and 4 were selected, and that the menu item that was selected was the second.

Processing forms

Now that we have the array that contains the form data we can process the data in it. The first thing to check is that the submit button has been pressed.

if($_POST['submitbutton']) //...
else //...

This code checks if the key 'submitbutton' has a value, if it does then the code in the if statement will run otherwise the else code will run.
The switch statement below is useful for processing radio buttons within a form.

switch($_POST['Radio1'])
{
   case 'Radiobutton1':
      echo 'Button 1 selected';
      break;
   case 'Radiobutton2':
      echo 'Button 2 selected';
      break;
   case 'Radiobutton3':
      echo 'Button 3 selected';
      break;
   default:
      echo 'No button selected';
      break;
}

The value of the array is checked against the known values and a simple message is printed.
You can check that the value selected within a menu against an array to check that the choice is from a certain set of values.

$menu = array('menuitem1', 'menuitem2', 'menuitem3');
if(in_array($_POST['select'], $menu))
   echo $_POST['select'];

The posted value is checked against a set of known values. Below are some string functions that can be useful for form validation.

if(strlen($_POST['textbox']) > 10) //length of string greater than 10?

echo nl2br($_POST['textarea']); //converts new lines in a text area into HTML line breaks

trim($_POST['textarea']); //removes white-space from the start and end of a string

Categories

Tags

No tags

Social