Validating a form using php

We will need this to format our inline messages later.

The rest of the code is simply handled as if it’s regular server side processing.

AJAX (Asynchronous Java Script and XML) is the art of exchanging data with a server, and updating parts of a web page – without reloading the whole page. We have already explained about form validation using different technologies.

validating a form using php-8

So every time we click the submit button, we go through each field in the form and create a data object.

This object contains the field names and their respective values (you can also use j Query’s .serialize() for this purpose).

All you have to do is just follow our codes or download it to use.

When in comes to validating forms, there are basically two techniques you can use: 1) Server-side validation and 2) Client-side validation.

Oh and of course, we will also have the good user experience of the page not refreshing when there’s errors. You can also have the processing page the same as the page where the form is. Copy the code below into your HTML: Nothing fancy here. Remember to make your field names unique – for this is what we’ll use to validate our form.

Keep in mind that you will need a bit of programming know how – especially in PHP and Javascript. So when our form submits, it passes the $_POST array to this page.

We make an Ajax call to our processing page – where it does the validation in our PHP code previously.

Now, remember our server side will echo a JSON object with our field names and error messages?

Let me know your thoughts and if you think this solution is a good fit for your projects.

Form Validation is very important technique when you want to send data to the server.

Update 4/2/2016: – The code below is not meant to be a “copy and paste” type of thing.

Tags: , ,