I am using a AJAX /php form .
if (strlen($name) < 4) { // If length is less than 4 it will output JSON error.
$output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"Naam" field contains invalid value')));
die($output);
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { //email validation
$output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"E-mail" field contains invalid value')));
die($output);
}
if (strlen($subject) < 3) { //check emtpy message
$output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"Onderwerp" field contains invalid value')));
die($output);
}
if (strlen($message) < 3) { //check emtpy message
$output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"Vraag" field contains invalid value')));
die($output);
}
The AJAX script :
$.post('contact_form', post_data, function (response) {
if (response.type == 'error') { //load json data from server and output message
output = '<div class="error">' + response.text + '</div>';
} else {
output = '<div class="success">' + response.text + '</div>';
//reset values in all input fields
$("#contact_form input[required=true], #contact_form textarea[required=true]").val('');
$("#contact_form #contact_body").slideUp(); //hide form after success
}
$("#contact_form #contact_results").hide().html(output).slideDown();
}, 'json');
The above code it valides form and output if is there any error. My issue is that i receive 1 error only. How can i receive all the errors in a json object ?