title: Password reset with secret questions using php ajax and mysql
body: <p><img src="https://i.stack.imgur.com/cIhku.jpg" alt="enter image description here">
  I'm trying to write a script which allow users to reset password after providing email address and answering secret questions. The problem is my secret question script is not working as expected.  When the user answer the secret questions it get posted to PHP script with the help of ajax and returns the responseText which should trigger ajax depending on returned response but this script here is always displaying else condition of ajax when the returned text meets some other conditions.... Any help will be very much appreciated. Thank you in advance for help..</p>
  
  <p>These are the step for resetting pasword:</p>
  
  <ol>
  <li>click on Forgot password</li>
  <li>Ask user email address</li>
  <li>Answer secret questions</li>
  <li>Send reset link in email</li>
  <li>After clicking on the link sent in email user will be directed to the password reset page where they can create new password.</li>
  </ol>
  
  <p>Here is the code </p>
  
  <pre><code>&lt;?php 
  
  if(isset($_GET['e'])){
          // CONNECT TO THE DATABASE
          include_once("php_includes/connect_to_mysqli.php");
          // GATHER THE POSTED EMAIL INTO LOCAL VARIABLES AND SANITIZE
          $email = mysqli_real_escape_string($db_conx, $_GET['e']);
  
          $sql = "SELECT * FROM useroptions WHERE email='$email' LIMIT 1";
          $query = mysqli_query($db_conx, $sql);
          $numrows = mysqli_num_rows($query);
          while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
              $id = $row["id"];
              $u = $row["username"];
              $q1 = $row["question"];
              $q2 = $row["question2"];
              $a1 = $row["answer"];
              $a2 = $row["answer2"];
  
                  }
            if ($q1 == "" || $q2 == ""){
                header ("location: messages.php?emsg=forget&amp;u=".$u);
                   exit();
                }
          }
  ?&gt;
  &lt;?php
  // AJAX CALLS THIS CODE TO EXECUTE
  if(isset($_POST["pa1"])){
      include_once("php_includes/connect_to_mysqli.php");
      $e = mysqli_real_escape_string($db_conx, $_POST['em']);
      $pa1= $_POST['pa1'];
      $pa2= $_POST['pa2'];
  
      $sql = "SELECT * FROM useroptions WHERE email='$e' LIMIT 1";
          $query = mysqli_query($db_conx, $sql);
          $numrows = mysqli_num_rows($query);
          while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
              $id = $row["id"];
              $a1 = $row["answer"];
              $a2 = $row["answer2"];
              }
  
  
      if ($pa1 == $a1 &amp;&amp; $pa2 == $a2 &amp;&amp; $e == $email ){
      $sql = "SELECT id, username FROM user WHERE email='$e' AND activated='1' LIMIT 1";
      $query = mysqli_query($db_conx, $sql);
      $numrows = mysqli_num_rows($query);
      if($numrows &gt; 0){
          while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
              $id = $row["id"];
              $u = $row["username"];
          }
          $emailcut = substr($e, 0, 4);
          $randNum = rand(10000,99999);
          $tempPass = "$emailcut$randNum";
          $hashTempPass = md5($tempPass);
          $sql = "UPDATE useroptions SET temp_pass='$hashTempPass' WHERE username='$u' LIMIT 1";
          $query = mysqli_query($db_conx, $sql);
          $to = "$e";
          $from = "auto_responder@yousite.com";
          $headers ="From: $from\n";
          $headers .= "MIME-Version: 1.0\n";
          $headers .= "Content-type: text/html; charset=iso-8859-1 \n";
          $subject ="yoursite Temporary Password";
          $msg = '&lt;h2&gt;Hello '.$u.'&lt;/h2&gt;&lt;p&gt; Email with activation link&lt;/p&gt;';
          if(mail($to,$subject,$msg,$headers)) {
              echo "success";
              exit();
          } else {
              echo "email_send_failed";
              exit();
          }
      }
      } else {
          echo "no_exist";
      }
  
      exit();
  }
  ?&gt;
  
  
  
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Security Answer-&lt;/title&gt;
      &lt;link rel="icon" href="favicon.ico" type="image/x-icon"&gt;
      &lt;link rel="stylesheet" href="css/styles.css"&gt;
      &lt;style type="text/css"&gt;
      #securityform{
          margin-top:24px;    
      }
      #securityform &gt; div {
          margin-top: 12px;   
      }
      #securityform &gt; input {
          width: 250px;
          padding: 3px;
          background: #F3F9DD;
      }
      #anssubmitbtn {
          font-size:15px;
          padding: 10px;
      }
      &lt;/style&gt;
      &lt;script src="js/main.js"&gt;&lt;/script&gt;
      &lt;script src="js/ajax.js"&gt;&lt;/script&gt;
      &lt;script&gt;
      function emptyElement(x){
          _(x).innerHTML = "";
      }
      function ajaxObj( meth, url ) {
      var x = new XMLHttpRequest();
      x.open( meth, url, true );
      x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      return x;
  }
  function ajaxReturn(x){
      if(x.readyState == 4 &amp;&amp; x.status == 200){
          return true;    
      }
  }
  
      function forgotpasscon(){
          var em = _("email").value;
          var pa1 = _("ans1").value;
          var pa2 = _("ans2").value;
          if(em == "" || pa1 == "" || pa2 == ""){
              _("status").innerHTML = "Answer all security questions";
          } else {
              _("anssubmitbtn").style.display = "none";
              _("status").innerHTML = 'please wait ...';
              var ajax = ajaxObj("POST", "testconfirm.php");
              ajax.onreadystatechange = function() {
                  if(ajaxReturn(ajax) == true) {
                      var response = ajax.responseText;
                      if(ajax.responseText == "success"){
                          _("securityform").innerHTML = '&lt;h3&gt;Step 2. Check your email inbox in a few minutes&lt;/h3&gt;&lt;p&gt;You can close this window or tab if you like.&lt;/p&gt;';
                      } else if(ajax.responseText == "no_exist"){
                          _("status").innerHTML = "Sorry wrong answers";
                      } else if(ajax.responseText == "email_send_failed"){
                          _("status").innerHTML = "Mail function failed to execute";
                      } else {
                          _("status").innerHTML = "An unknown error occurred"+ajax.responseText;
                          _("anssubmitbtn").style.display = "block";
                      }
                  }
              }
              ajax.send("em="+em+"&amp;pa1="+pa1+"&amp;pa2="+pa2);
          }
      }
      &lt;/script&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
      &lt;?php include_once("template_pageTop.php"); ?&gt;
      &lt;div id="pageMiddle"&gt;
          &lt;h3&gt;Step : 2&lt;/h3&gt;
        &lt;h4&gt;Please answer the following security questions!!&lt;/h4&gt;
        &lt;form id="securityform" onsubmit="return false;"&gt;
          &lt;div&gt;Question 1:&lt;/div&gt;
          &lt;p&gt;&lt;?php echo $q1; ?&gt;&lt;/p&gt;
          &lt;input name="ans1" id="ans1" type="text" onfocus="_('status').innerHTML='';" maxlength="100"&gt;
          &lt;br/&gt;&lt;br/&gt;
          &lt;div&gt;Question 2:&lt;/div&gt;
          &lt;p&gt;&lt;?php echo $q2; ?&gt;&lt;/p&gt;
          &lt;input name="ans2" id="ans2" type="text" onfocus="_('status').innerHTML='';" maxlength="100"&gt;
          &lt;br /&gt;&lt;br /&gt;
          &lt;input name="email" id="email" type="hidden" value="&lt;?php echo $email; ?&gt;" /&gt;
  
          &lt;button id="anssubmitbtn" onclick="forgotpasscon()"&gt;Submit&lt;/button&gt; 
          &lt;p id="status"&gt;&lt;/p&gt;
        &lt;/form&gt;
      &lt;/div&gt;
      &lt;?php include_once("template_pageBottom.php"); ?&gt;
      &lt;/body&gt; 
      &lt;/html&gt; 
  </code></pre>