bembry.org
Home / Technology / Web_development / Php

PHP Form Processing Basics

What Happens
A form consists of two parts, a page with an actual form for folks to fill out, and another page of script that takes the information entered and does something with it, then displays something on the screen for the user to see. The data entered into a form can be submitted to a database, tested to see if it matches certain criteria, emailed to anybody on the planet, added to a dynamic web page, or any number of other options depending on how the script itself is written.

Variables
In a form, each <input> must be given a name, as follows:

<input type="text" name="user_name"> <input type="checkbox" name="wants_email">

When the form is submitted to a PHP script, the assigned name automatically becomes a PHP variable containing whatever value the user entered on the form. When the sample code above is submitted to a PHP script, it creates two variables, $user-name and $wants_email. If the user submitted the user name "Goofy Gus", then in the PHP script, $user_name = "Goofy Gus". If he clicked to have email sent to him, then $wants_email = ON (or TRUE or however you want to think about this Boolean operator).

Displaying Variables
To display the values a user entered on the form, use the "echo" command as follows:

<?php
echo "<h1> Hello $user_name </h1>";
?>

With this little script, the web page now looks customized, saying "Hello Goofy Gus" in great big letters. Any variable can be displayed on screen in this manner.

YES/NO Tests
Since form input data is stored as variables, it is also possible to have PHP read the variable information, test it, and make a determination on what to do next. For example, the script can test if the "wants_email" box has been checked, then display the appropriate message depending on whether the user checked this box or not. To do this, we would use the if() funtion to see if the box is checked, then provide the options we want as follows:

<?php
if ($wants_email)
echo "Thanks for signing up for the email";
else
echo "Okay, so you don't want my spam";
?>

Since the script is testing a checkbox, which is either "ON" or not, then if($wants_email) will be TRUE when the box is checked and FALSE when it is not checked. If it is checked, the users get the first message. If it is not, then PHP skips the first message and goes on to the "else" section, displaying the second message.

In the code, notice the placement of the semicolons. They should come at the end of the line that tells the script what to do under each condition, not at the end of the test lines "if()" and "else".

Comparison Tests
In the above example, the script simply tests to see if a variable is turned on or off. It is also possible to use the if() function to see if the user's input, or the value returned by a form, matches a string of text. Perhaps we want to test if the user entered the correct password in the $user_name field above. Here is the script to do that, assuming that the password is "TuxRacer":

<?php
if ($user_name == "TuxRacer")
include "secretfile.inc";
else
echo "Thank you for your interest. To join our secret club, please email me."; ?>

In this script, PHP first compares the user's input in $user_name with the text string "TuxRacer". If the user typed in "TuxRacer", then the script includes the file "secretfile.inc" as part of this page. Otherwise, the script prints out the "Thank you" statement. The script demonstrates that if() function can be used to run any valid PHP code, whether it be echo, include, mail(), or anything else. This is a powerful way to create alternative experiences based on user input.

Notice in the script above that the if() function uses two equals signs (==). To test a value we have to use the two equals signs, otherwise the script would say "$user_name="TuxRacer", effectively replacing the user's input with the value "TuxRacer". Also, note that if someone entered the user name "TUXracer", it would not be the same as "TuxRacer", since PHP is case sensitive.

Multiple Options
Tests are ideal for using with input tags that have designated values, such as buttons, radio inputs, and options. In each of these, the input value is set by the programmer in the original form code and passed on to the script. To set the value of an input, use the VALUE= option inside the input tag. For example:

<input type="radio" name="seasonal" value="annuals">
<input type="radio" name="seasonal" value="perennials">
<input type="radio" name="seasonal" value="evergreens">

In each of the above inputs, the web designer has designated a specific VALUE for the input information. When the form is passed to the PHP script, the variable will be set to the values specified in the tag. If the user chose "perennials", then the script woudl receive the following: $seasonal = "perennials".

When a script has more than two choices to check for, then PHP uses the format "if ... else if ... else". In the following code, the PHP script checks to see which choice the user made and then includes the appropriate selection of code.

<?php
if ($seasonal == "annuals")
include "annuals.inc";

elseif ($seasonal == "perennials")
include "perennials.inc";

elseif ($seasonal == "evergreens")
include "evergreens.inc";

else
echo "Please go back and select an appropriate choice."
?>

Notice in the above code the spelling of "elseif" (all one word, lower case). In the code, the PHP script checks to see if one of the three choices was selected (annuals, perennials, or evergreens) and then, if none of them were selected, it prints out an error message. Whenever building code, it is important to include error checking routines and messages like this.

Restricted access