Skip to content
Advertisement

how to display form errors under input field with php and javascript

I have a form. I want to submit the form without reloading the page. so I am using ajax to send data to PHP and then validating the inputs with PHP storing the errors in an array. How can I return error array to javascript and show errors in input fields.

Any Suggestions?

JavaScript

PHP

JavaScript

Javascript

JavaScript

Advertisement

Answer

At first change this portion of the php file

JavaScript

with this

JavaScript

This (json_encode(…)) will convert the php variable to a json encoded string. Now you will get back the string as result (responsetext) on client side.

At client side you can convert the string to a JSON object using JSON.parse(result); and get the errors back to a string (js string).

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement