title: php ajax jquery responseText not returned
body: <p>I am trying to write an insert query with jquery, ajax and php. The record is getting inserted but returns a status error. First I tried to <code>echo</code> the message in php as it didn't work I tried it with <code>print json_encode</code> but both returned the status as error. Why doesn't it return the responseText?</p>
  
  <blockquote>
    <p>{readyState: 0, responseText: "", status: 0, statusText: "error"}</p>
  </blockquote>
  
  <p>This is the addmember.php file </p>
  
  <pre><code>&lt;?php
       require '../database.php';
  
      function random_password( $length = 8 ) {
              $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&amp;*()_-=+;:,.?";
              $password = substr( str_shuffle( $chars ), 0, $length );
              return $password;
               }
  
  
      $password = random_password(8);
      //$regno=$_POST['regNo'];
      $adminno=$_POST['adminNo'];
      $batch=$_POST['batchText'];
      $type=$_POST["memberType"];
      $initials=$_POST["initialName"];
      $fullname=$_POST["fullName"];
      $address=$_POST["address"];
      $telephone=$_POST["contact"];
      $email=$_POST["email"];
      $nic=$_POST["nic"];
      $dob=$_POST["birthDate"];
      $priv=$_POST["memberType"];
      $userid="";
  
      $sql="select username from memberinfo where username='$adminno'";
      $result=mysqli_query($con,$sql); 
      if(mysqli_num_rows($result)==0){
          $sql="insert into memberinfo(username,nic_no,class,name_initial,full_name,address,telephone,email,date_of_birth) VALUES ('$adminno','$nic','$batch','$initials', '$fullname', '$address', '$telephone','$email','$dob')";
          $result1=mysqli_query($con,$sql);   
          $sql = "select * from memberinfo where username='$adminno'";
                                  $result = $con-&gt;query($sql);
                                  if ($result-&gt;num_rows &gt; 0) {
                                      // output data of each row
                                      while($row = $result-&gt;fetch_assoc()) {
  
                                          $userid = $row['user_id'];
                                      }
                                  }
  
          $sql="insert into userlogin(user_id,username,privilege,password) VALUES ('$userid','$adminno','$priv','$password')";
          $result2=mysqli_query($con,$sql);   
  
          if ($result1 &amp;&amp; $result2) {
  
                 $message = "&lt;p&gt;New record created successfully&lt;/p&gt;";
  
           } else {
                  $message = "&lt;p&gt;Error: " . $sql . "&lt;br&gt;" . $con-&gt;error.".&lt;/p&gt;";
          }
      } else{
          $message = "&lt;p&gt;Admission no already exists.&lt;/p&gt;";
      }
  
      print json_encode($message); 
  
  
       $con-&gt;close()                
  
  
  ?&gt;
  </code></pre>
  
  <p>This is the .js file with the ajax function</p>
  
  <pre><code>$(document).ready(function(){
  
      $('#addmember').click(function(){
          console.log("addmember");
          var adminno=$("#adminNo").val();
          var nic=$("#nic").val();
          var batch=$("#batchText").val();
          var initials=$("#initialName").val();
          var fullname=$("#fullName").val();
          var address=$("#address").val();
          var telephone=$("#contact").val();
          var email=$("#email").val();
          var dob=$("#birthDate").val();
          var priv=$("#memberType").val();
  
                          //$("#result").html("&lt;img alt='ajax search' src='ajax-loader.gif'/&gt;");
                           $.ajax({
                              type:"POST",
                              url:"../ajax/addmember.php",
                              dataType: "json",
                              data:{'adminNo':adminno, 'nic':nic,'batchText':batch,'initialName':initials, 'fullName':fullname, 'address':address, 'contact':telephone,'email':email,'birthDate':dob,'memberType':priv},
                              success:function(response){
  
                                  console.log(response);
                                  $("#result").append(response);
                               },
                               error:function(response){
                                  console.log(response);
                               }
                            });
  
      });
  
  
  
  });
  </code></pre>