title: No message when data entry success
body: <p>The following code successfully submits data to a mysql database. However, the form does not show error when fields are blank and it will not populate the div with the success message when it successfully inserted the data. The php file below shows that if insert is a success the result variable is set to 1 which is reflected in the condition in the if else conditions for the ajax.</p>
  
  <pre><code>&lt;body&gt;
   &lt;div id = "container" &gt;
     &lt;h3&gt;jQuery AJAX Post method example with php and MySQL&lt;/h3&gt;
     &lt;p&gt;&lt;strong&gt;Please fill in the form and click save.&lt;/strong&gt;&lt;/p&gt;
     &lt;div id="message"&gt;&lt;/div&gt;
      &lt;form name='form1'&gt;
        &lt;table align = ""&gt;
          &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
           &lt;tr&gt;
              &lt;td&gt;
                &lt;label&gt;First Name:&lt;/label&gt;&amp;nbsp;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;input type='text' placeholder='First Name' name='first_name' id= 'first_name' required &gt;&lt;br /&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;
                  &lt;label&gt;Last Name:&lt;/label&gt;&amp;nbsp;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;input type='text' placeholder='Last Name' name='last_name' id='last_name' required &gt;&lt;br /&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;
                &lt;label&gt;Email:&lt;/label&gt;&amp;nbsp;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;input type='email' name='email' placeholder='Email' id='email' required &gt;&lt;br /&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td&gt;
                &lt;input class='btn' type="button" id = "saveusers" value = "Save" /&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
    &lt;/div&gt;
    &lt;script src="http://code.jquery.com/jquery-3.1.1.min.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
      $(function(){
          $("#saveusers").on('click', function(){
              var first_name  = $("#first_name").val();
              var last_name   = $("#last_name").val();
              var email       = $("#email").val();
  
              $.ajax({
                method: "POST",
                url:    "saverecords_ajax.php",
                data: { "first_name": first_name, "last_name": last_name, "email": email},
               }).done(function( data ) {
                  var result = $.parseJSON(data);
                  var str = '';
                  var cls = '';
                  if(result == 1) {
                    str = 'User record saved successfully.';
                    cls = 'success';
                  }else if( result == 2) {
                    str = 'All fields are required.';
                    cls = 'error';
                  }else if( result == 3) {
                    str = 'Enter a valid email address.';
                    cls = 'error';
                  }else{
                    str = 'User data could not be saved. Please try again';
                    cls = 'error';
                  }
                $("#message").show(3000).html(str).addClass('success').hide(5000);
            });
         });
       });
  
    &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>saverecords_ajax.php file</p>
  
  <pre><code>&lt;?php
  
  $host         = "localhost";
  $username     = "root";
  $password     = "password";
  $dbname       = "testing";
  $result = 0;
  
  /* Create connection */
  $conn = new mysqli($host, $username, $password, $dbname);
  /* Check connection */
  if ($conn-&gt;connect_error) {
       die("Connection to database failed: " . $conn-&gt;connect_error);
  }
  
  /* Get data from Client side using $_POST array */
  $fname  = $_POST['first_name'];
  $lname  = $_POST['last_name'];
  $email  = $_POST['email'];
  /* validate whether user has entered all values. */
  if(!$fname || !$lname || !$email){
    $result = 2;
  }elseif (!strpos($email, "@") || !strpos($email, ".")) {
    $result = 3;
  }
  else {
     //SQL query to get results from database
     $sql    = "insert into users (first_name, last_name, email) values (?, ?, ?)  ";
     $stmt   = $conn-&gt;prepare($sql);
     $stmt-&gt;bind_param('sss', $fname, $lname, $email);
     if($stmt-&gt;execute()){
       $result = 1;
     }
  }
  echo $result;
  $conn-&gt;close();
  </code></pre>