Browser-side checks improve feedback, but server-side validation remains authoritative. FormData can send the same field names as a normal form submission.
const form = document.getElementById('signupForm');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const messageBox = document.getElementById('msgDsp');
try {
const response = await fetch(form.action, {
method: 'POST', body: new FormData(form)
});
if (!response.ok) throw new Error('Request failed');
const result = await response.json();
const check = result.data[0];
messageBox.textContent = check.msg;
messageBox.style.borderColor = check.status_form === 'OK' ? 'green' : 'red';
} catch (error) {
messageBox.textContent = 'Validation service unavailable. Try again.';
}
});The linked demo uses XMLHttpRequest to illustrate the request-response steps. The fetch() example above is an alternative for the same JSON validation endpoint. Both must handle server validation results and request failures.
AJAX sends form values to a PHP receiver without reloading the document. The receiver validates values and returns JSON, and JavaScript updates the existing form. Validation does not automatically create an account or save data.
ajax-form-validation-demo.php.
new URLSearchParams(new FormData(form)) to collect all controls and encode them safely without a custom getFormData() function.
// Manual encoding of a text field for application/x-www-form-urlencoded.
var val = myForm.fname.value;
myParameters.push('fname=' + encodeURIComponent(val));
// Read the selected value from the radio group.
var val = '';
for (var i = 0; i < myForm.sex.length; i++) {
if (myForm.sex[i].checked) val = myForm.sex[i].value;
}
myParameters.push('sex=' + encodeURIComponent(val));
// Read the selected option from a dropdown.
var val = myForm.month.options[myForm.month.selectedIndex].value;
myParameters.push('month=' + encodeURIComponent(val));
yes in this demo.
// Unchecked checkboxes have no value in submitted form data.
if (myForm.agree.checked) {
myParameters.push('agree=' + encodeURIComponent(myForm.agree.value));
}
When assembling the request manually, join the encoded parameters with & and send them with a POST request. Alternatively, FormData and URLSearchParams avoid manual encoding mistakes.
msgDsp message region to display validation feedback. You can read more on how to manage message while using Ajax here. data array. Its first item contains r1, t1, s1, c1 and status_form. A value of F marks an invalid field. For example:
// The PHP response sets r1 to F if the radio selection is invalid.
const invalidRadio = myObject.data[0].r1 === 'F';
document.getElementById('r1').style.borderColor = invalidRadio ? 'red' : 'white';
The server returns a flag for each field group. Apply a visible validation style only when its flag is F; provide a text explanation too so color is not the only feedback. You can learn more on CSS border style here. aria-invalid.
OK and a short success message. The demo displays its result with a green border.
NOTOK and a plain-text message. Use textContent when displaying that message:
// Treat server messages as text; never inject user-influenced HTML.
const result = myObject.data[0];
const messageBox = document.getElementById('msgDsp');
messageBox.style.borderColor = result.status_form === 'NOTOK' ? 'red' : 'green';
messageBox.textContent = result.msg;
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.