Check email availability in Cakephp using Ajax
<p>I am trying to check the availability of email through Ajax in Cakephp but it is not working.The form submits normally and adds the duplicate email address in database.Following is the code of template and controller file.</p>

<p><strong>View.ctp</strong>   </p>

<pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;script&gt;
  $(document).ready(function(){
    $("#email").click(function(){
      $("#email").blur(function(){
        var email = document.getElementById("email").value;
        $.ajax({
           type: "POST",
           url: "/login/users/checkmail",
           data: {email:email},
           success: function(result){
             if(result == 'ok'){
             document.getElementById("myForm").submit();
           }else{
              alert('Email already exists');
            }
         }
       });
       return false;
     });
   });
 });
&lt;/script&gt;
&lt;?= $this-&gt;Form-&gt;create('User',['enctype' =&gt; 'multipart/form-data'],array('id'=&gt;'myForm')) ?&gt;
  &lt;fieldset&gt;
    &lt;?= $this-&gt;Form-&gt;input('username') ?&gt;
    &lt;?= $this-&gt;Form-&gt;input('email',array('id'=&gt;'email')) ?&gt;
    &lt;?= $this-&gt;Form-&gt;input('password') ?&gt;
    &lt;?= $this-&gt;Form-&gt;input('file',array( 'type' =&gt; 'file')) ?&gt;
    &lt;?= $this-&gt;Form-&gt;button(__('Register')) ?&gt;
</code></pre>

<p></p>

<p><strong>UsersController.php</strong> </p>

<pre><code>public function checkmail(){

if($this-&gt;request-&gt;is('ajax')){
    $user = $this-&gt;request-&gt;data['email'];
    $users = $this-&gt;Users-&gt;find('all')-&gt;where(['email'=&gt; $user]);
    $tmp = $users-&gt;toArray();
    if(empty($tmp)){
        echo 'ok';
    }else{
        echo '!ok';
    }

}
exit;
}
</code></pre>