| Home / Technology / Web_development / Php | |
![]() |
PHP Form Processing Basics
What Happens
Variables
<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
<?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
<?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
<?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
<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")
elseif ($seasonal == "evergreens")
else 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 |