title: PHP Jquery Ajax Post data not saving to database
body: <p>I'm studying jquery ajax php post, I'm getting the success message but the data is not saving on my database, the page is online please see it on the link below.</p>
  
  <p><a href="http://marlz13.net78.net/Feedback%20AJAX/" rel="nofollow">http://marlz13.net78.net/Feedback%20AJAX/</a></p>
  
  <p>Here is my Send JS script:</p>
  
  <pre><code> $(document).ready(function() {
  
   $('form #response').hide();
  
   $('#submit').click(function(e) {
  
  // prevent forms default action until
  // error check has been performed
  e.preventDefault();
  
  // grab form field values
  var valid = '';
  var required = ' is required.';
  var name = $('form #name').val();
  var email = $('form #email').val();
  var message = $('form #message').val();
  var honeypot = $('form #honeypot').val();
  var humancheck = $('form #humancheck').val();
  
  // perform error checking
  if (name = '' || name.length &lt;= 2) {
      valid = '&lt;p&gt;Your name' + required +'&lt;/p&gt;';  
  }
  
  if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
      valid += '&lt;p&gt;Your email' + required +'&lt;/p&gt;';                                                  
  }
  
  if (message = '' || message.length &lt;= 5) {
      valid += '&lt;p&gt;A message' + required + '&lt;/p&gt;';    
  }
  
  if (honeypot != 'http://') {
      valid += '&lt;p&gt;Spambots are not allowed.&lt;/p&gt;';    
  }
  
  if (humancheck != '') {
      valid += '&lt;p&gt;A human user' + required + '&lt;/p&gt;'; 
  }
  
  // let the user know if there are erros with the form
  if (valid != '') {
  
      $('form #response').removeClass().addClass('error')
          .html('&lt;strong&gt;Please correct the errors below.&lt;/strong&gt;' +valid).fadeIn('fast');           
  }
  // let the user know something is happening behind the scenes
  // serialize the form data and send to our ajax function
  else {
  
  $('form#response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');                                       
  
      var formData = $('form').serialize();
      submitForm(formData);       
   }          
  
   });
   });
  
  // make our ajax request to the server
  function submitForm(formData) {
  
  $.ajax({    
  type: 'POST',
  url: 'feedback.php',        
  data: formData,
  dataType: 'json',
  cache: false,
  timeout: 7000,
  success: function(data) {           
  
      $('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
                  .html(data.msg).fadeIn('fast'); 
  
      if ($('form #response').hasClass('success')) {
  
          setTimeout("$('form #response').fadeOut('fast')", 5000);
      }
  
   },
   error: function(XMLHttpRequest, textStatus, errorThrown) {
  
      $('form #response').removeClass().addClass('error')
                  .html('&lt;p&gt;There was an&lt;strong&gt; ' + errorThrown +
                        '&lt;/strong&gt; error due to a&lt;strong&gt; ' + textStatus +
                        '&lt;/strong&gt; condition.&lt;/p&gt;').fadeIn('fast');           
   },             
   complete: function(XMLHttpRequest, status) {           
  
      $('form')[0].reset();
   }
   });    
   };
  </code></pre>
  
  <p>Here's the PHP script</p>
  
  <pre><code>  &lt;?php 
    // Establishing connection with server..
    $connection = mysql_connect("databaseserver", "username", "password");
  
   // Selecting Database 
   $db = mysql_select_db("db", $connection);
  
   sleep(2);
   //Sanitize incoming data and store in variable
   $name = trim(stripslashes(htmlspecialchars($_POST['name'])));          
   $email = trim(stripslashes(htmlspecialchars($_POST['email'])));
   $message = trim(stripslashes(htmlspecialchars($_POST['message'])));        
   $humancheck = $_POST['humancheck'];
   $honeypot = $_POST['honeypot'];
  
   if ($honeypot == 'http://' &amp;&amp; empty($humancheck)) {    
  
      //Validate data and return success or error message
      $error_message = '';    
      $reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";
  
      if (!preg_match($reg_exp, $email)) {
  
                  $error_message .= "&lt;p&gt;A valid email address is required.&lt;/p&gt;";             
      }
      if (empty($name)) {
  
                  $error_message .= "&lt;p&gt;Please provide your name.&lt;/p&gt;";              
      }           
      if (empty($message)) {
  
                  $error_message .= "&lt;p&gt;A message is required.&lt;/p&gt;";
      }       
      if (!empty($error_message)) {
                  $return['error'] = true;
      $return['msg'] = "&lt;h3&gt;Oops! The request was successful but your form is not filled out correctly.&lt;/h3&gt;".$error_message;                 
                  echo json_encode($return);
                  exit();
      } else {
  
              //Insert query 
   mysql_query("INSERT into formdata (name, email, desc) values ('$name', '$email', '$message')");
  
          //connection closed
          mysql_close($connection);   
  
          $return['error'] = false;
          $return['msg'] = "&lt;p&gt;Thanks for your feedback " .$name .".&lt;/p&gt;"; 
  
          echo json_encode($return);
  
  
  
        }     
   } else {
  
   $return['error'] = true;
   $return['msg'] = "&lt;h3&gt;Oops! There was a problem with your submission. Please try again.&lt;/h3&gt;"; 
   echo json_encode($return);
   }
  
   ?&gt; 
  </code></pre>
  
  <p>This is on the head of my HTM </p>
  
  <pre><code>&lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
   &lt;title&gt;AJAX form tutorial using jQuery and PHP&lt;/title&gt;
  &lt;link href="styles.css" rel="stylesheet" type="text/css" /&gt;
  &lt;script type="text/javascript"      src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="ajax_submit.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  </code></pre>