Server Side Registration Form Validation by PHP


Download Source Code

A sample Registration Form Server Side validation using PHP


In this article I am trying to present a basic HTML form elements and how their data is accessible and processing by the PHP scripts. It is very essential to have the input to your form validated before taking the form submission data for further processing.

Client-side form validations help in giving immediate feedback to the user but it is also required to add server side form validation form to processing script. The user can disable JavaScript on their server or even auto-bots might try to submit your form as well. The server side form validations help keeping the form submission data consistent and it also help in lesser server side errors.

The form contains a number of inputs: text fields, radio buttons, select list, a checkbox, and a submit button. These inputs will be validated to ensure that the user has entered their input is correctly and any errors or omissions will be highlighted with a message alongside the relevant field.

Form Creation

Here I have create a simple Registration Form that has input field Name, Email, Password, Address, Country, phone, comment and a web address field that is enable by the clicking Yes radio button and also a Agree checkbox and at last a submit button. Right side of each input field a warning box is placed where php field error will be thrown.

Registration Form outlook

Text Fields

Name, Email, Password, Address fields are input text field. Here sample code segment for the Name field is,

Here I have taken three table columns for each table row. First table is allocated for the label for the input field, second for the input text and third is for the input field php error message.

The HTML input element can have several attributes, the most important are name, type and value attributes. type=”text” defines these instances as a single-line input field to accept text. The name attribute is used to specify a name for this value. And value field is contains the value that you enter the input text field.

Web-URL Radio Button

Here to input the web-Address, you should select radio button “Yes” that is by default is “No”. So two things are associated with the Web-Address input field, One is select the radio button “Yes” to enable the input text field and another is type the web-URL within the input text box. So the web-URL text field will be conditionally enable or disable according to the select of radio button in the onclick. So the code segment for the radio button is as following,

Select List

For the country field, I have created a dropdown list that code segment is as following,

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

The Form Element:

All of the form control elements must be enclosed within the form tags. ‘action’ and ‘method’ attributes are need to be set in the form tag.

htmlspecialchars() is used here to convert specific HTML characters to their HTML entity names, e.g. > will be converted to >. This prevents the form from breaking if the user submits HTML markup and also is a means of protecting against XSS (Cross Site Scripting) attacks. “action” defines where the form contents are sent when the form is submitted; in this case, the value of $_SERVER[“PHP_SELF”]. $_SERVER is an array with entries filled in at PHP run time; the PHP_SELF key contains the filename of the PHP script that is being executed. Using $_SERVER[“PHP_SELF”] the form elements script is executed in the same page where html form element is exists.

‘method’ determines how the form’s contents is submitted. POST means that the form’s content is sent as part of the HTTP request’s body. The values are then retrievable in PHP using the $_POST array.

Form HTML Segment

The complete code segment for the HTML Form element is as following,

Adding Style

The style that I have been added for the complete HTML Form is following,

Server Side Validation

When the form is submitted, the values of each form elements will be stored in the $_POST array (or $_GET array depending on the form’s method attribute). The values in the left-hand column are taken from the control’s name attribute, and I’ve also marked whether or not the field is a required field for validation purposes.

Here I have used the extract($_POST) to get all of the data from the each field. The extract() function imports variables into the local symbol table from an array. This function uses array keys as variable names and values as variable values. For each element it will create a variable in the current symbol table. This function returns the number of variables extracted on success.

Name Input Field

For the Name field I have checked the three case is as, if the field is empty (as Name field is mandatory) or if the Name character length is greater than 10 or if the Name string is doesn’t match with the predefine Name pattern then an error message will be thrown.

A form field validation

Email Address

For the Email field I have checked the three case is as, as Email field is mandatory so, if the Email field is empty then an error will be thrown or if the Email character length is greater than 30 or if the field string is doesn’t match with the predefine Email pattern then an error message will be thrown.

Email field validation

Web Address

Here web address field will be mandatory when you click the “Do you have web address” radio button is ‘yes’. So when radio button value is yes then Web URL entry field should not be empty, and Web URL length and pattern should be matched with the correct format or else the error will be thrown. If the radio button value is ‘no’ then Web URL field doesn’t not thrown error.

Web Address validation

Password Field

For the Password field I have checked the three case is as, as Password field is mandatory so, if the Password field is empty then an error will be thrown or if the Password character length is greater than 15 character or if the Password string is doesn’t match with the predefine Password pattern then an error message will be thrown.

Password field validation

Address Field

For the Address Field, I have checked only one case is that if the address character length is greater than 200 character then an error message will be thrown.

Address field validation

Country Dropdown

As country field is mandatory so you should select a country name. By default the value is ‘none’ for the dropdown list text ‘— select country —’. So if you don’t select any country name the value will be ‘none’ and an error will be thrown.

Country Input field validation

Phone Field

For the phone field you should entry the character less than 15 and each character should be a digits or else it will be thrown an error message.

Phone field validation

Comment box

For the comment box field, if the comment length is greater than 500 than an error message will be thrown.

Agree Checkbox

You must be agree with terms and condition check box to submission complete this Form.

Agree checkbox validation

After Filter Data

After checking each field error, if the field value is okay then for the each field return a value 0 that means it has no error in the field value. So is all of the field values are okay then you can do any desire operation for the further operation.


Validation is essential, particularly if you are going to save the data in a database. In this article I am trying to present how to create a simple HTML form and validate it with PHP. Along the way a number of techniques have been used to re-display user input and display error messages. So after got the fresh data you can do any desire operation with those data. So keep going and Have a better coding….

Previous articleVideo Creation Software Video Maker Fx with Crack
I am a professional software developer from last 4 years. I have experience in analysis, design, development, testing and implementation of desktop (using C, C++, C#) and web (using HTML5, CSS3, JavaScript, PHP, MySql) platform using. I have good exposure to object-oriented design, software architectures, design patterns, test-driven development and Project Management.