Processing a form
1. Use a hidden field in the form to check to see whether a form has posted. Then your PHP program can check to see if the form needs displaying or processing.
2. Use $_POST['form_variable'] to get the value of a variable entered in the form.
The general form a program to display and process a form is therefore as follows:
If hidden field has no value set
display the form
else if hidden field has a value
process the form, using $_POST['variable']
Example (without layout elements)
Here's an example of a PHP program that displays and processes a form. It's a simple program to give you the idea of wht to do. A more detailled, more realtic example is given in the more detail section on the right.
Form:
<input type="text" size="20" name="form_name">
<input type="text" size="20" name="form_address">
<input type="text" size="20" name="form_postcode">
<input type="submit" value="Go!">
<input type="hidden" name="form_posted" value="posted">
PHP program:
<?php
if (!array_key_exists('form_posted',$_POST)) { Form has not posted because form_posted has no value
?> <input type="text" size="20" name="form_name">
<input type="text" size="20" name="form_address">
<input type="text" size="20" name="form_postcode">
<input type="submit" value="Go!">
<input type="hidden" name="form_posted" value="posted">
<?php
} else { form_posted has got a value, so process the form
echo "Your name is "; echo $_POST['form_name'];
echo "Your address is "; echo $_POST['form_address'];
echo "Your postcode is "; echo $_POST['form_postcode'];
}
Next article: Functions