Writing PHP - 4: Forms

No need for CGI access

PHP can handle forms internally - there is no need to write or setup a Perl script, no need for cgi-bin access and no need to learn an extra language. You can write a PHP file to receive the form and process it, or you can store the special PHP variable called $_SERVER["PHP_SELF"] in a temporary variable (in this case $self = /php/form.php) and post the form back to the same script! To post a form back to itself, just create a hidden field in the form. If the variable from that field is NOT set, then the form has not been submitted. If it is set, the script should go ahead and process the contents of the form. Consider this form:

<form method="post" action="$self"><div>
Name: <input type="text" name="myname" value="$myname">
<input type="hidden" name="os" value="ds">
<input type=submit value="Go!"></div>

This code results in the following form: (Check the source of this page for confirmation.) Enter your name and click Go! for a demonstration.


Enter your name and it will be displayed here once you click Go!

PHP creates matching variables for all input fields in the form where a name is specified. These variables will be stored in two special arrays - $_POST[] and $_GET[]. GET[] refers to the query string in the URL - the bit after the ?. POST[] contains data from the submitted form where the method attribute is set to post. To retrieve the values of the variables, use $variable = POST["variablename"]; In this form, the variable $os is being used in both the posted form data and the query string, so a little bit of logic is required to allow the GET[] setting to override the POST[]. This is to allow a link on this page to reset the form by using the query string and GET[] to clear the $os variable. If the query string does not contain a value for $os, then $os is set to the value from the posted form data, if any.

$os = $_GET["os"];
if(!(isset($os))) {
	$os = $_POST["os"];
$myname = $_POST["myname"];

Note that the myname input has a default value set as $myname. When PHP outputs this form, it will insert the current value of $myname as the default value for the input itself. This is very useful for long forms where the visitor forgets to enter a required field - the rest of the fields can be restored automatically, a kind of auto-complete or form-memory. The Demo form (with or without cookies) demonstrates the use of default values by restoring the entire form. All that remains for the sample form on this page is to add a simple 'if' command to check if the form has already been submitted:

if ($os=="ds") {
echo "Your name is $myname.";}

You don't have to re-display the form, it all depends on how you use the if ($os="ds") {} test. You can put the test at the top of the file and generate a completely different page - using the values from the form. The form itself then resides inside an else {} block - if the os=ds test fails, the else block is executed and the form is displayed. In effect, one PHP file contains 2 or more pages. This can also be used to force a reset of the form. Note that if you use default values as a form-memory, when the visitor clicks your Reset button, the form will NOT be cleared - the Reset button will simply reset each field to the default value - i.e. it will undo any changes and re-instate the memorised form. To clear the fields within the form, you can use the QUERY_STRING - the strange bit at the end of a WWW URL which includes lots of ? & = and + signs. The os=ds test can be used to clear the entire form by forcing the test to fail. So even though the form has been submitted, if you submit a different value for os it will override the value from the previous form and the script will display a new form with no default values set.

<a href="$self?os=0">Reset form</a>

The query-string is the os=0. Any PHP file can receive a query-string in this way. The value of the variable will be stored in $_GET[] using the name given in the query string, so $os is retrieved from $_GET["os"]. So in the PHP file, $os is set to zero. The os=ds test now fails, so the script outputs a new form - just as if the form hadn't been submitted at all. The first time the PHP file is loaded, $os has no value. If you test for a variable that has no value assigned or has not been created, PHP will return the same result as if the value was assigned to zero. A better way of testing for this is to use !(isset($os)). The !(isset()) means to test for the existence of the variable and if the variable does not exist, return true. View these tests before and after submitting the form and after resetting the form.

# no $os variable exists
if (!$os) {echo "true"}  # true
if ($os==0) {echo "true"}  # true
if (!(isset($))) {echo "os has not been set" } # os has not been set

# create the $os variable and assign zero
# (without clearing any existing setting.)

if (!(isset($os))) { $os=0; }

# test again

if (!$os) {echo "true"}   # true
if ($os==0) {echo "true"}  # true
if (!(isset($os))) {echo "os has not been set" } # os is set $os = 0

[ Reset the form ] [ Show the form ]

This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.