0

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.

enter image description here I tried below code

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?

questionbank
  • 440
  • 8
  • 25
  • @charlietfl, I added alert after success alert(data.error); and I got [object Object] in the alert. – questionbank Aug 11 '19 at 17:26
  • Well don't use alert for debugging....log to console instead so you can inspect that object. Also show what gets returned from controller in `else` – charlietfl Aug 11 '19 at 17:27
  • @charlietfl, I added console.log(data.error); and I got all the errors which are in the screenshot of the image. – questionbank Aug 11 '19 at 17:29
  • @charlietfl, What I did, I remove the foreach code from controller and added $errors_val = validation_errors(); $response['error']=$errors_val; In the console I am getting the proper output but now how do I display the error message on the specific field. – questionbank Aug 11 '19 at 17:34

0 Answers0