Form is used to collect data from user. For example any type of registration or feedback collection or any login, forms are used. Forms are also used to create Dynamic Applications. Dynamic applications means when you login to a website, then your own profile will be displayed just like facebook. In some website, admin changes the content of whole website without the knowledge of coding, that type of websites are also known as Dynamic applications.
HTML is used to design a form. But programming languages like PHP are used to collect data from form & also helps to store it in Databases. In HTML two methods are used to send data such as GET & POST method. To receive data $_GET & $_POST Global variables are used in PHP. When form method is GET, $_GET will be used to collect data & similarly when form method is POST, $_POST will be used to collect data from user end.
POST method is recommended to use while sending data from a form because GET is a unsecure method to send data. All information with form variables names are displayed on the URL while sending data with GET.
Use of $_GET[ ]
Lets design a form with the following details
Next Enter values in form & Press SUBMIT button
After press SUBMIT button, following output will display on URL
The output will be display on same page because action attribute is not used with the form. Just look at URL, form value along with variable name is displayed on the URL. Even if password characters are visible on URL if GET method is used.
Now lets print the value from URL by using PHP. After pressing the submit button, the value will be print on the screen. So for that isset() function is used to check when the user click on the submit button.
Here the name of Submit button “butSubmit” with global variable $_GET[ ] is pass as a parameter of isset() function. When the user press the button the value of isset() function return 1. 1 means TRUE. So if() return TRUE and enters to the if block. First we have to print name. So we have to write the following code in if block.
echo is used to print the value & the variable txtName with $_GET[ ] is used to print the value of variable txtName that passed in the URL. If we run that code it will only print the Name entered in the form.
Similarly we can print the value of other variables that are passed on URL.
USE OF $_POST[ ]
Now solve the above example with the help of global variable $_POST[ ]. In form we have change the method from GET to POST. Then enter values & press submit button.
Here we have to replace the $_GET[ ] with $_POST[ ] like below
In POST method, no value and variable are display in the URL. So its secure to use with form. The output will be same as above.
DISPLAY FORM VALUE WITH $_POST[ ] IN OTHER PAGE
Now we are using a ACTION attribute with form code. Form method is POST as in the above example.
Let our form in Page1.php & we want the output in Page2.php. So in action attribute we have the page.php. Now enter the value & press Submit button.
In page2.php, we have to write PHP code to print the output.
The above code will print the value on output.
Validation is a restriction on Form components. That means form validates which type of value entered by a user. In other words, when a form submitted by a user, at that time form checks the values that are entered by the user is in correct form or not also alert the user that they messed up something & need to fix to proceed.
A) NON-EMPTY VALIDATION
Non-empty validation means it check the the form components has some value or not. So lets take an example. First create form like give below
The above form has two components, one is text box & another one is submit button. The name assign to the text box is “txtName” and to the submit button is “txtSubmit”. For non-empty validation the code is given below
Here the empty() will check the value of the given form components(txtName) is empty or not. If the form components is empty, it will print “Name is required” other wise it will print the value entered in the Name components.
B) LETTER VALIDATION
Letter validation means the entered characters are letters or not. So first to create a Form like above. For letter validation the code is given below.
Here preg_match() function is used to match the enter string with a pattern. The pattern also created by the user according to the requirement. To create a pattern different symbols are used. Lets explain the above example.
Double Quotes (“ “) – All the characters inside the quotes are the part of the pattern but itself the double quotes are not.
Forward slash (/ ….. /) – Two forward slashes, one at the beginning and another at the end are known as delimiter. It states that for where the pattern starts & ends.
Caret(^) – Beginning of the string
Square Bracket ([ ])– In square bracket, the letter validation syntax are explained. For small letters a-z is used, for capital letters A-Z, for space Single quote(‘) is used.
Star(*) – Repetition of characters.
Dollar($) – End of the string
In the above example, the designed pattern will match with the input field “txtName”. A not symbol(!) is used at beginning of the preg_match(). That means if the entered value is not match with the Pattern, it will enter to the True block & print the message “Only Letters and White spaces allowed” otherwise it will print the value entered by the user.
C) NUMBER VALIDATION
Number Validation is same as Letter Validation. Only the syntax inside the [ ] bracket will be changed as given below.
In the above example, the designed pattern will match with the input field “txtNum”. A not symbol(!) is used at beginning of the preg_match(). That means if the entered value is not match with the Pattern, it will enter to the True block & print the message “Only Numbers allowed” otherwise it will print the value entered by the user.
D) EMAIL VALIDATION
Email validation means it validates the email entered by the user is in correct format or not. Lets design a form like below.
For email validation, the code is given below
In the above example filter_var() is used to compare the entered string with a keyword FILTER_VALIDATE_EMAIL. If the comparison is true that means the entered string is a valid email id. But in given example a not(!) operator is used before filter_var(). Means if the entered string & the keyword not match, it enters to the TRUE block & prints “Invalid email format” otherwise it prints the Email id.