0

I am using CodeIgniter. I am using jquery remote method to identify the email id already register or not. I tried some code it's working but my doubt is how to display the validation error message on view page?

I am getting the output on the network tab.

Note: I am not talking about the PHP. I know the process will be same but in my case, There error message not displaying on the view page.

HTML

<input type="email" class="form_control" id="email" name="email" value="<?php echo set_value('email'); ?>">

jquery validation

email: {
        required: true,
        email:true,
        remote: {
        url: baseUrl + "/Employee_control/checkemail_exist",
        type: "post",
        data: {
        email: function () {
              return $("#email").val();
                    }}
                 }
            },

I already added

messages: {
        email: {remote: "This email is already registered with us!"
      }
    },

Controller

public function checkemail_exist(){
   $email= $this->input->post('email');
    $email_check=$this->Employee_model->checkemail($email);
    if ($email_check == 0) {
      //echo "not exist";
      return true;
    }
    else{
    //echo " exist";
    return false;

    }

  }

Model

 public function checkemail($email){
    $this->db->select('email_id');
    $this->db->where('email_id',$email);
    $query = $this->db->get('tbl_employee');
    $result=$query->result();
    if (empty($result)) {
      return 0;
    }
    else{
      return 1;
    }

    }
user9437856
  • 2,360
  • 2
  • 33
  • 92
  • return only true or false from your controller – Pradeep Jun 25 '18 at 19:46
  • @pradeep, Yes, I tried. I commented my echo but still not displaying the error message. – user9437856 Jun 25 '18 at 19:47
  • `echo`, not `return`, a JSON encoded string. If pass, `echo "true"`, if fail, `echo "false"`. [Refer to the documentation: *"The serverside response must be a JSON string that must be `"true"` for valid elements, and can be `"false"`, `undefined`, or `null` for invalid elements, using the default error message. If the serverside response is a string, eg. `"That name is already taken, try peter123 instead"`, this string will be displayed as a custom error message in place of the default."*](https://jqueryvalidation.org/remote-method/) – Sparky Jun 25 '18 at 20:29
  • @Sparky, I don't have any issue with PHP, The link which you shared it PHP, My issue is with CodeIgniter and how to display the error message on the view page? that's my issue. There is no issue with remote function – user9437856 Jun 26 '18 at 04:52
  • CodeIgniter is 100% PHP, and the link tells you how to properly use remote with PHP. – Sparky Jun 26 '18 at 06:15
  • @Sparky, Yes, I know how to use remote. It's working in PHP. I tried on my last project. Now I am working on CodeIgniter and I am doing the same step but still not able to display the error message on the view page.Evening I am getting the output in the network tab – user9437856 Jun 26 '18 at 06:23
  • Yet you’re still doing it wrong, and did not follow the documentation or linked answer. – Sparky Jun 26 '18 at 14:13
  • 2
    @Sparky, Yes, Now I understand the issue.I updated controller if ($email_check == 0) { $valid = 'true'; } else{ $valid = 'false'; } echo $valid; and it's working – user9437856 Jun 26 '18 at 14:59

0 Answers0