Return Server-side Validation Results as JSON

Return a consistent JSON validation response

Use one response shape for success and failure so the browser does not have to parse custom text formats.

<?php
header('Content-Type: application/json; charset=utf-8');
// Check types before reading values; do not suppress notices with @.
$fname = is_string($_POST['fname'] ?? null) ? trim($_POST['fname']) : '';
$row = ['t1' => 'T', 'status_form' => 'OK', 'msg' => ''];
if (!preg_match('/^.{3,80}$/uD', $fname)) {
    $row['t1'] = 'F';
    $row['status_form'] = 'NOTOK';
    $row['msg'] = 'Enter a name between 3 and 80 characters.';
}
echo json_encode(['data' => [$row]]);
?>
Current practice: keep client-side validation for user experience, but always repeat validation on the server. For database input, use PDO or MySQLi prepared statements.

This excerpt shows the response format. The linked ajax-form-validation-demo-code.php validates all four fields and returns the same data[0] structure.

In the first part of form validation we have seen how the Ajax form and the functions we have used to collect data from forms and then process it to handover to PHP file by using POST method. After getting the data we will write PHP code to validate the data and generate the message.

The POST receiver reads the four fields only after checking their types. Form fields can also arrive as arrays, so avoid passing unchecked values directly to string functions.
// The complete endpoint first rejects array-valued fields.
$fname = is_string($_POST['fname'] ?? null) ? trim($_POST['fname']) : '';
$sex   = is_string($_POST['sex'] ?? null) ? $_POST['sex'] : '';
$month = is_string($_POST['month'] ?? null) ? $_POST['month'] : '';
$agree = is_string($_POST['agree'] ?? null) ? $_POST['agree'] : '';
Initialize each field flag as passed (T). If any rule fails, change the appropriate flag to F, collect the messages, and set status_form to NOTOK.
$row = [
    't1' => 'T', 'r1' => 'T', 'r2' => 'T',
    's1' => 'T', 'c1' => 'T',
    'msg' => '', 'status_form' => 'OK'
];
$messages = [];
The response array groups the status of each field with the overall form status. The browser reads these flags to mark the corresponding fields.
Now we will start with checking the data which as come to us as fname for First name. We have set a validation check as it must me minimum 3 char in length. You can change your validation checking as per your requirement. It can be alphanumeric or it can be only alphabets or you can add maximum length to this. The example uses a Unicode-aware regular expression for the permitted length; see also PHP string length.
if (!preg_match('/^.{3,80}$/uD', $fname)) {
    $row['t1'] = 'F';
    $messages[] = 'Enter a name between 3 and 80 characters.';
}
// Validate sex, month and agree in the same way.
if ($messages !== []) {
    $row['status_form'] = 'NOTOK';
    $row['msg'] = implode("\n", $messages);
}
The full endpoint also validates the radio choice, month and agreement checkbox. It returns one JSON object, whether the form passes or fails. The server accepts only the two defined radio values, a month from 01 to 12, and a checked agreement value of yes. The client displays the JSON result as plain text and highlights invalid field groups.
demo of PHP Ajax form validation
This demo validates values without storing them. A real registration system would separately need duplicate-user checks, database prepared statements, CSRF protection for state-changing actions, and a clear post-submit flow.

Download the code of this demo




Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer