PHP Action form
PHP Action form
To familiarize students with form information processing and validation in PHP
After this lab the students should be able to retrieve the information submitted by the user through form and apply server-side validation.
Introduction
Forms provide a mean of submitting information from the client machine to the server.
Method and action are the most common attributes of the <form>. Action is used to give the URL of the application that is to receive and process the forms data. Method sets the HTTP method that the browser uses to send the form’s data to the server for processing. Get and post are the common methods. PHP automatically makes few variables available in your program. These variables are called super-global variables because they can be accessed without regard to scope. The most commonly used super global variables are $_GET and
$_POST. $_GET super global variable contains all the query string variables that were attached to the URL. When we use get method in a form, its input is transferred along with the URL as query string parameters therefore, $_GET variable also keeps the input of the form when it is sent by get method. $_POST is also an array type variable which is created automatically by PHP. It contains all the submitted form variables and their data. When we use post method in the form, form‟s input data is stored in the $_POST super global array which can be retrieved on the action page.
When a form is submitted; the control is transferred to the action page (referred in the action attribute of the form tag). The code on the action page can process the user‟s input. We can retrieve the user‟s input on the action page from $_GET or $_POST (depending on the method we used in the form) variables. The values can be retrieved as:
$_POST[„name of the input field given in the form‟]; or
$_GET[„name of the input field given in the form‟];
For example; if we used the get method in the form and input field is created as:
<input type=”text” name=”email”> then we can retrieve its value as
$_GET[„email‟];
Example:
<body>
<form method=“post” action=”action.php”> <input type=“text” name=“name”> <input type=“text” name=“email”> <input type=“submit”> </form> </body> |
action.php
<?php echo $name = $_POST[„name‟]; echo $email = $_POST[„email‟]; ?> |
Validating user’s input:
Once the data is received we can use PHP to validate the user‟s input. In PHP usually we declare a regular expression and the use a PHP function (preg_match()) to compare user‟s input with the regular expression.
A regular expression is a concise notation to describe patterns in strings. Regular expressions provide the foundation for describing or matching data according to defined syntax rules.
Notations used to declare regular expressions:
- ^: match strings that start with the given pattern
- $: match strings that end with the given pattern
- -: means a range of characters
- [ ]: makes a class of characters
- [^ ]: negates the class of character
- ?: matches the character, class or sub-pattern 0 or 1 time equal to {0,1}
- +: matches the character, class or sub-pattern 1 or more times equals to {1, }
- *: matches the character, class or sub-pattern 0 or any number of time equals {0, }
- \d: means exactly as [0-9]
- \D: means exactly as [^0-9]
- \w: means exactly as [a-zA-Z0-9]
preg_match(): searches a string for a specific pattern returns TRUE if it exists and FALSE otherwise preg_match(“pattern”,$string);
Example:
<body>
<form method=“post” action=”action.php”> <input type=“text” name=“name”> <input type=“text” name=“email”> <input type=“submit”> </form> </body> |
action.php
<?php $name = $_POST[„name‟]; $email = $_POST[„email‟]; if(!preg_match(“|^[a-zA-Z0-9_.]+@[a- z]{3,5}.[a-z]{3}$|”,$email)) echo “Invalid Email address”; ?> |
Lab Activities:
Activity 1:
Create a HTML form as given below. Write the PHP script that retrieves the user‟s input and validate it as defined below
- All input fields with * are mandatory
- First and last name must contain alphabets only
- Email address must be a valid email address
- CNIC number should also be a valid CNIC number (i.e. 12345-1234567-1)
- The comment should have at-least 5 words
Solution:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ /> <title>Forms Lab</title> </head> <body> <form name=”htmlform” method=”post” action=”reg_action.php”> <table width=”450px” border=”0″ bgcolor=”#6699FF” align=”center”> <tr> <th valign=”top” colspan=”2″ bgcolor=”#666699″> User Registration Form </th> </tr> <tr> <td valign=”top”> <label for=”first_name”>First Name *</label> </td> <td valign=”top”> <input type=”text” name=”first_name” maxlength=”50″ size=”30″> </td> </tr> <tr> <td valign=”top””> <label for=”last_name”>Last Name *</label> </td> <td valign=”top”> <input type=”text” name=”last_name” maxlength=”50″ size=”30″> </td> </tr> <tr> <td valign=”top”> <label for=”email”>Email Address *</label> </td> <td valign=”top”> <input type=”text” name=”email” maxlength=”80″ size=”30″> </td> </tr> <tr> <td valign=”top”> <label for=”first_name”>CNIC No. *</label> </td> <td valign=”top”> <input type=”text” name=”cnic” maxlength=”50″ size=”30″> </td> </tr> <tr> <td valign=”top”> <label for=”telephone”>Telephone Number</label> </td> <td valign=”top”> <input type=”text” name=”telephone” maxlength=”30″ size=”30″> </td> </tr> <tr> <td valign=”top”> <label for=”comments”>Comments on Your Self *</label> </td> <td valign=”top”> <textarea name=”comments” maxlength=”1000″ cols=”25″ rows=”6″></textarea> </td> </tr> <tr> <td colspan=”2″ style=”text-align:center”> <input type=”submit” value=”Submit”> </td> </tr> </table> </form> </body> </html> |
PHP Code
<?php if(!isset($_POST[‘first_name’]) || !isset($_POST[‘last_name’]) || !isset($_POST[’email’]) || !isset($_POST[‘cnic’]) || !isset($_POST[‘comments’])) { die(‘We are sorry, but there appears to be a problem with the form you submitted.’); } $first_name = $_POST[‘first_name’]; $last_name = $_POST[‘last_name’]; $email = $_POST[’email’]; $cnic = $_POST[‘cnic’]; $telephone = $_POST[‘telephone’]; $comments = $_POST[‘comments’]; $error_message = “”; $email_exp = ‘/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/’; if(!preg_match($email_exp,$email)) { $error_message .= ‘The Email Address you entered does not appear to be valid.<br />’; } $name_exp = “|^[a-zA-Z]{2,10}$|”; if(!preg_match($name_exp,$first_name)) { $error_message .= ‘The First Name you entered does not appear to be valid.<br />’; } $cnic_exp = “|^[0-9]{5}-[0-9]{7}-{0-9}{1}$|”; if(!preg_match($cnic_exp,$cnic)) { $error_message .= ‘The CNIC NO. you entered is not valid.<br />’; } if(!preg_match($name_exp,$last_name)) { $error_message .= ‘The Last Name you entered does not appear to be valid.<br />’; } if(str_word_count($comments)< 5) { $error_message .= ‘The Comments you entered do not appear to be valid.<br />’; } if(strlen($error_message) > 0) { echo $error_message; } echo “Form details below.<br>”; echo “First Name: “.$first_name .”<br>”; echo “Last Name: “.$last_name.”<br>”; echo “Email: “.$email.”<br>”; echo “CNIC No.: “. $cnic.”<br>”; echo “Telephone: “.$telephone.”<br>”; echo “Comments: “.$comments.”<br>”; ?> |
Home Activities:
Activity 1: Write the HTML code that creates a form as given below. Write the PHP scripts that validates the input provided by the user.