Creating HTML Form with PHP Server Side Validation.
October 15, 2016 Comments 0 425 Views

Creating HTML Form with PHP Server Side Validation.

This article will explain how you can create a HTML Form and do server side validation for the form. So the form which will display will be plain HTML and the processing of the form after submit will be done by PHP.

HTML Form Preparation for display.

We will take a form example which will contain Textfield / Radio Button / Check box / Select Box which are commonly used in form.

The Form Element :

Form always start with the form element which have “Method” defines the method on how the form will be submitted. Method can be GET or POST the GET method send all data in URL query string which is less preferred and unable to handle very large data. POST method is the preferred one which post form data and can handle large form data for posting to the require page. Another parameter you can see in below form element is “Action” this define the path of the page where the form get submitted to. It can be the same page OR can be different page.

Text Fields :

For example we will take 4 text fields which are Name, Address, Phone and Email fields. Fields will be text input elements and can be coded like below example :

Checkbox :

For the checkbox the input type is “checkbox” where we provide pre-defined value like for below example it is set to “Yes” if the box has been checked we will get “Yes”

Radio Button :

Radio button are defined as type=”radio” which renders the input as a radio button. In case of radio button we need to define the same name to radio button in one group because collectively the radio buttons should be treated as a group (the user should select either Male or Female). The value attribute will be different to provide the different selections information which was selected by user.

Select List ( Multiple Choices ) :

With Select list field we can display Options to select which can be single or multiple. We will check multiple option where the name attribute of the select field is given as an array like interestedin[] in below example (having square brackets) . Also because of the presence of the “multiple” attribute in below select field user can select multiple options. Without multiple, only one option can be selected.

Each option specified within the select has a unique value. The value attribute for each option element is the value that will be submitted to the server for processing, and the text between the opening and closing option tag is the value the user will see in the option list.

Submit Button :

Finally we need to provide button for the user which can be hit to submit the form for processing after selecting and entering details in all form fields.

Now as we have our form ready we will write a PHP code to the top of the page to process the form.

Processing Form using PHP code :

Now we know that we have used the form submission method in form tag as “POST” so we will get all the form submitted values in array variable name as $_POST we will take the values from this array and validate. Also we will set all the values received and array occured in different variable for processing. Below is the PHP code for validation.

Above code will validate the form. To extend your form which will show the values pre-filled if anything goes wrong and show error below fields you can follow below method where in value you can echo the variable and below the field you can echo the error in span tab with srror class for user display.

Using the aboe method you can retain data filled by the user and display specific errors in your form. Below is the complete example.

Hope this article will help you in creating HTML form and process it using PHP. You can send the mail or save details to database if there are no errors. This was very basic example which will give a basic idea on how things work and will get your form going. If you find this article help ful then share the article with your friends on social network.

Previous Remove Emoji Script & CSS from your wordpress source code.
Next PHP function to sort array alphabetically or numerically in ascending or descending order.

About author

Rio
Rio 37 posts

Expert web developer working in PHP, Wordpress, Joomla, Magento, Javascript etc.

You might also like

Working with PHP Array – Indexed, Associative & Multi-Dimensional

PHP Array are variables which can hold more than one value at a time. For example if you want to store 1000 numbers then instead of defining 1000 variables its easy

Working With PHP & Databases

Working with PHP & Database require basic knowledge of below items : Database Connection Inserting Data Reading Data Updating Data Deleting Data Making Database Connection : Using PHP you can

Secure directory by allowing access to IP address & Deny all using htaccess

Your website may have folders which you want to keep secure access using URL. For example Admin area you want to keep secure to access using particular IP address and

PHP 7 Using New Operators

PHP 7 also brings us some new operators. Let’s just cut the talk and directly check how PHP 7 Using New Operators. Spaceship Operator : The spaceship operator, or Combined Comparison

Get list of Images from a Folder using PHP

With PHP you can get list of all images available under any particular folder. You can use preg_grap function to check file extension and get list of file having required

ZIP Files & Folder using Simple PHP Code

Using PHP you can Zip Files and Folders with a simple php code, which will provide you with the functionality to create zip automatically without manual efforts. Simple PHP code to

Store passwords securely in PHP to prevent password hacking

While creating an application using PHP there are maximum chances that you have login for which you have to store user data which also contain passwords for login. We will

How to detect website is access from Mobile using PHP.

Todays trend shows that maximum number of website access using mobile is increased. People are using smart phone to access the mobile instead of their desktop OR laptop. Because mobile

Using Type Declarations in PHP 7

PHP is considered to be a weak typed language. Using type declarations in PHP 7 simply means specifying which type of variable is being set instead of allowing PHP to