The key to the code above is the check for an Ajax request. This is when it’s an Ajax request and we simply echo our error array in JSON format.We will need this to format our inline messages later.Note that in our case – I’m simply checking if first name and email is empty.Of course in the real world, you will have to check for valid email and such.Now, remember our server side will echo a JSON object with our field names and error messages?
Of course, we have add our styling so our field will look like our screenshots above. Again, the styles are real basic and you probably want to do better in your own forms. We’ve combined best of both worlds: server side and client side validation in one solution (thanks to Ajax). If a field validation rule has to change – we only need to change it in one place: the server code.You would probably want to style this better, or even echo it out in the same page as the form.But for the sake of our tutorial, I want to make it short and concise.When in comes to validating forms, there are basically two techniques you can use: 1) Server-side validation and 2) Client-side validation.Server-side validation is when form data is submitted, server analyzes then returns the user back to the form when items are invalid.