PHP MySQL tutorial

Form Validation in PHP

Form validation refers to the process of validating if the the data enter by the user in the form corresponds to a correct format. Form validation include empty field validation, correct input format check and length check etc. Form validation can be implemented on client as well as server side. On client side, form validation is implemented via JavaScript or JQuery. On server side validation is implemented via some server side language. PHP also provides form validation functionality. This article throws light on it. The best way to understand form validation is via an example.

Order Form Validation

Item: Price: Quantity:

In the above code, a form has been created which contains, item name, price and quantity fields. When the form is submitted the data is sent to the ordervalidation.php page via POST method. The validations are performed in that page. The contents of ordervalidation.php are as follows:

The name of the item cannot be empty.

"; } if(empty($price)){ echo "

The price of the item cannot be empty.

"; } if(empty($quantity)) { echo "

The quantity cannot be empty"; } if($quantity < 5) { echo "

The quantity should be greater than 5

"; } ?>

Download the code
Run the code

In the above the values passed in the first example are received via POST method. Each value is stored in the variable using the $_POST associative array. Next, using the empty function it is checked whether any of the fields is empty or not. If the fields are empty, an appropriate message is displayed to the user. At the end the quantity field is validated against a particular value.

Form validation is a extremely extensive topic. You can perform all sorts of form validation ranging from email syntax validation to sequence validation and so on.

<<< File Creation and Writing in PHPDate and Time Functions in PHP >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .