I am receiving valid json response when the form is not submitted (when the validation fails)
I can print it in console. I am struggling to show that validation error message to the HTML form under or next to the proper field.
my form
<form action="" method="post" name="contact-me" id="profile-update" class="requires-validation">
<div class="d-flex flex-row align-items-center mb-4">
<i class="fas fa-key fa-lg me-3 fa-fw"></i>
<div class="d-flex form-floating mb-0 flex-fill">
<input name="name" type="text" class="form-control name" placeholder="Type your name" >
<label for="name" class="form-label">Your Name</label>
<div class="nameerror">Name field is valid!</div>
</div>
</div>
<div class="d-flex flex-row align-items-center mb-4">
<i class="fas fa-key fa-lg me-3 fa-fw"></i>
<div class="d-flex form-floating mb-0 flex-fill">
<input name="email" type="email" class="form-control" placeholder="Type a valid email" >
<label for="email" class="form-label">Your Email</label>
<div class="emailerror">Name field is valid!</div>
</div>
</div>
<div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
<button type="submit" class="btn btn-primary btn-lg" id="submit">Send message!</button>
</div>
<div id="nds_form_feedback"></div>
</form>
So I want to show the name validation error message in <div class="nameerror">Name field is valid!</div> and email validation error message in <div class="emailerror">Name field is valid!</div>
my validation
<?php
function prfl_update_validation_func(){
$errors = array();
$response = array();
$yourname=$_POST['yourname'];
$yourname=$_POST['email'];
if ((empty($_POST['yourname'])) ) {
$errors['yourname'] = "Please enter your name";
}
if ((!empty($_POST['yourname'])) && (!preg_match("/^[a-zA-Z-' ]*$/",($_POST['yourname'])))) {
$errors['yourname'] = "Special characters or spaces are not allowed in the username";
}
if ((empty($_POST['email'])) ) {
$errors['email'] = "Please enter your email";
}
$response['errors'] = $errors;
if($errors) {
$response['success'] = false;
$response['message'] = "Fail";
} else {
$reponse['success'] = true;
$response['message'] = "<div class='alert alert-success'>Article added</div>";
}
echo json_encode($response);
wp_die();
}
So all my form validation errors are in $response['errors'] = $errors; right?
now, Ajax part. please read the commented lines as well
<script type="text/javascript">
jQuery(document).on('click', '#submit', function(e){
e.preventDefault();
var fd = new FormData();
fd.append('action', 'prfl_update_validation_func');
jQuery.ajax({
type: 'POST',
url: ajax_url,
data: fd,
contentType: false, //because I have a file upload feild as well
processData: false, //because I have a file upload feild as well
headers: {Accept : "application/json;charset=utf-8"},
debug:true,
success: function(response) {
if(response.type == "success") {
jQuery("#nds_form_feedback").html('Sucessfully Sent'); // if the form passes the validation this works fine
}
else {
alert("Not Uploaded");
jQuery("#nds_form_feedback").html('boooooooooooo'); // this replace the div #nds_form_feedback correctly if there is a validation error
jQuery("#nds_form_feedback").html("<ul><li>"+errors.email+"</li></ul>"); // this says 'errors' is not defined
console.log(response);
}
}
});
});
</script>
what I want here is showing the correct validation error message under/next to it’s input fields.
jQuery("#nds_form_feedback").html("<ul><li>"+errors.email+"</li></ul>"); this needs to be replaced with correct classes and with correct array values.
[EDIT]
json response I am getting in console
{
"errors": {
"yourname": "Please enter your name",
"email": "Please enter your email"
},
"success": false,
"message": "Fail"
}
Please help.