Condition not working and form submitting even though email already exists

Viewed 63

I am trying to check email avaliablilty using AJAX and PHP, but I am facing the following two problems:

1) If I'm getting response true or false showing me "Email available" message, means if else condition is not working with response.

2) The form still submitting if email already exist.

Here is my code in view:

<script>
$(document).ready(function(){
    $("#email").blur(function(){
      var email = $("#email").val();
    $.ajax(
    {
        type:"post",
        url: "<?php echo base_url(); ?>index.php/Register/check_emails",
        data:{ email:email},
        success:function(response)
        {
            alert($.trim(response));
        if($.trim(response) == "true")
            {
                alert("trueeeee");
                $('#lgmsg').html('<span style="color:red;">Valid Email</span>');
            }
            else 
            {
                alert("falseeee");
                $('#lgmsg').html('<span style="color:green;">Email avaliable</span>');
            }  
        }
    });

  });
});
</script> 

Here is my controller function where I am getting response/result

public function check_emails()
    {
        $email = $this->input->post('email');
        $record['data']=$this->Home->check_emailsd($email);
        if($record['data']=="exist")
        {
            echo "true";
        }
        else
        {
            echo "false";
        }
    }
0 Answers
Related