jQuery function to check if an email address exists
<p>I have a very limited jQuery experience and I was wondering if you can help me with a function that has to check, with an AJAX request, if an email address exists or not. </p>

<p>Until now I have this piece of code for email checking:</p>

<pre><code>$('input#email').bind('blur', function(){
  $.ajax({
    url: 'ajax/email.php',
    type: 'GET',
    data: 'email=' + $('input#email').val(),
    cache: false,
    success: function(html) {
      if (html == 1) alert('Email exists!');
    } 
  });
});
</code></pre>

<p>How can I make a function out of this and use it like this:</p>

<pre><code>if (!email_exists($('input#email').val())) {
   $('#error_email').text('Email exists').show();           
   return false;
}
</code></pre>

<p>My PHP code looks like this:</p>

<pre><code>$email = ($_GET['email']) ? $_GET['email'] : $_POST['email'];

$query = "SELECT `id` FROM `users` \n"
       . "WHERE `users`.`email` = '".mysql_real_escape_string($email)."'";     
$result = mysql_query($query); 

if (mysql_num_rows($result) &gt; 0) {  
    echo '1';
} else {
    echo '0';
}
</code></pre>

<p>Thank you.</p>
