title: check if email exist in database using jquery validate plugin
body: <p>I am trying to submit form to database using ajax. I have sucessfully managed to post to database using AJAX. But not the trouble is I am not sure how to check if the submitted value already exist in database.</p>
  
  <p>I already have php that does so, But I am trying to use AJAX. I am using <strong>jquery validate plugin</strong> Following is the code.</p>
  
  <pre><code>$('#subscribeForm').validate({
    errorElement: "p",
    errorPlacement: function(error, element) {
    $('.status').html(error);
  },
  
  rules: {
    email: {
      required: true,
      email: true,
      remote: {
          url: 'index.php',
          type: 'post',
          data: {
              email: function(){
              // not sure how to check this
          }
      }
  }
  }
  },
  
  messages: {
  email: {
  required: "Please enter your email.",
  email: "Please enter a valid email.",
  // remote: jQuery.format("{0} is already in use")  // doesnt work
  }
  },
  
  submitHandler: function(form) {
  var btn = $(".submit_button");
  btn.attr("value", "Submitting..");
  
  $(form).ajaxSubmit({
      success: function(){
          btn.attr("value", "Submitted");
      }
  });
  return false;
  }
  });
  </code></pre>
  
  <p>This is my PHP:</p>
  
  <pre><code>if(isset($_REQUEST['subscribeForm'])){
  $email = trim($_POST['email']);
  
  if(empty($email) || !valid_email($email)){
      $status = 'Please provide valid email address.';
  } else {
      if(email_exist($email)){
          $status = 'You are already subscribed.';
      } else {
          add_email($email);
          $status = 'Thank you for subscribing.';
      }
     }
   } 
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>This is what I have tried but again with fail.</p>
  
  <pre><code>if(isset($_REQUEST['subscribeForm'])){
  $email = trim($_POST['email']);
  
  if(empty($email) || !valid_email($email)){
      $status = 'Please provide valid email address.';
  } else {
      if(email_exist($email)){
          $status = 'You are already subscribed.';
          echo false;
      } else {
          add_email($email);
          $status = 'Thank you for subscribing.';
          echo true;
      }
  }
  } 
  </code></pre>
  
  <p>And my Javascript</p>
  
  <pre><code>rules: {
  email: {
  required: true,
  email: true,
  remote: {
    url: 'index.php',
    type: 'post'
     }
  }
  },
  
  messages: {
   email: {
  required: "Please enter your email.",
  email: "Please enter a valid email.",
  remote: "You are already subscribed."
     }
   },
  </code></pre>