I am using Codeigniter. I am getting the issue on server-side validation error message using ajax. Server-side validation is not displaying properly. Gender field is required but I am getting the server-side error message on all the field. Also, check I am getting the id name.
I have to display the error message on the specific field which is required.
js
my success code
success: function(data) {
if (data.error == "true") {
swal({
title: "Success",
text: data.msg,
icon: data.icon,
type: "success"
}).then(function() {
window.location.href = baseUrl + "/Menu_control/confirmList";
});
} else {
$.each(data.error, function(key, value) {
var element = $('#' + key);
element.after(value);
});
}
},
Controller
$response = array('success' => false, 'error' => array());
if ($this ->form_validation-> run() == FALSE) {
foreach($_POST as $key => $value) {
$response['error'][$key] = validation_errors($key);
}
} else {
//submit code here
}
echo json_encode($response);
I remove foreach from the controller and added below code. In the console, I am getting the correct output now how do I display below of error message below of the field?
$errors_val = validation_errors();
$response['error']=$errors_val;
Would you help me out with this issue?