title: Ajax & PHP email with JSON return
body: <p>I have a simple contact form. I got the invalid email and fill all the fields error messages correctly, but I don't get the success message. Hence it's sending the email twice without giving any returning success messages (I just click once, I'm sure about that).</p>
  
  <p>The JS part: </p>
  
  <pre><code>&lt;script language="javascript" type="text/javascript" &gt;
      $(function(){
          $("#ContactForm").submit(function(){
              $("#submitf").value='Sending...';
  
              $.post("send.php", $("#ContactForm").serialize(),
              function(data){
                  if(data.frm_check == 'error'){
  
                      $("#message_post").html("&lt;div class='errorMessage'&gt;Error: " + data.msg + "!&lt;/div&gt;");
                      document.ContactForm.submitf.value='Send Again &gt;&gt;';
                      document.ContactForm.submitf.disabled=false;
                  } else if(data.frm_check == 'done') {
                      $("#message_post").html("&lt;div class='successMessage'&gt;Thanks, " + data.msg + "!&lt;/div&gt;");
                  }
              }, "json");
  
              return false;
          });
      });
   &lt;/script&gt;
  </code></pre>
  
  <p>The PHP part: </p>
  
  <pre><code>$return_arr = array();
  $email = $_POST["email"];   
  $message= $_POST["message"];    
  $name= xss_protect(sacarXss($_POST["name"]));   
  if(!empty($email) &amp;&amp; !empty($name) &amp;&amp; !empty($message)) {
      if(isValidEmail($email)){   
          $return_arr["frm_check"] = 'done';
          $return_arr["msg"] = "Success";             
          send_mail($email, $name, $message);
      } 
      else{
          $return_arr["frm_check"] = 'error';
          $return_arr["msg"] = "Invalid email";        
      }
  
  } else {                
      $return_arr["frm_check"] = 'error';
      $return_arr["msg"] = "Fill all the fields.";
  }
  echo json_encode($return_arr); ?&gt;
  </code></pre>
  
  <p>The HTML part: </p>
  
  <pre><code>&lt;form method="post"  id="ContactForm"&gt;
      &lt;div class="element"&gt;
          &lt;input type="text" name="name" class="text" placeholder="name" /&gt;&lt;br /&gt;
          &lt;input type="text" name="email" placeholder="email" class="text" /&gt;&lt;br /&gt;
          &lt;textarea name="message" class="textarea" rows="3" placeholder="message"&gt;&lt;/textarea&gt;&lt;br /&gt;
          &lt;input type="submit" name="submitf" id="submitf" value="send!"/&gt;
      &lt;/div&gt;
      &lt;div id='message_post'&gt;&lt;/div&gt;
  &lt;/form&gt;
  </code></pre>