title: change password using PHP, Ajax and Jquery
body: <p>I think i need someone with a better eye to help me spot my error. I am trying to change the current password without refreshing the page. I get the error message "Error, please try again!". This is my code so far.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;form name="resetform" action="changepass.php" id="resetform" class="passform" method="post" role="form"&gt;
    &lt;h3&gt;Change Your Password&lt;/h3&gt;
    &lt;br /&gt;
    &lt;input type="hidden" name="username" value="&lt;?php echo $sname;?&gt;" &gt;&lt;/input&gt;
    &lt;label&gt;Enter Old Password&lt;/label&gt;
    &lt;input type="password" class="form-control" name="old_password" id="old_password"&gt;
    &lt;label&gt;Enter New Password&lt;/label&gt;
    &lt;input type="password" class="form-control" name="new_password" id="new_password"&gt;
    &lt;label&gt;Confirm New Password&lt;/label&gt;
    &lt;input type="password" class="form-control"  name="con_newpassword"  id="con_newpassword" /&gt;
   &lt;br&gt;
   &lt;input type="submit" class="btn btn-warning" name="password_change" id="submit_btn" value="Change Password" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  include_once 'database-config.php';
  
  if (isset($_POST['password_change'])) {
  
      $username = strip_tags($_POST['sname']);
      $password = strip_tags($_POST['old_password']);
      $newpassword = strip_tags($_POST['new_password']);
      $confirmnewpassword = strip_tags($_POST['con_newpassword']);
  
  // match username with the username in the database
      $sql = "SELECT * FROM users WHERE username='$sname'";
  
      $query = $dbh-&gt;prepare($sql);
  
      $query-&gt;execute();
  
      $row = $query-&gt;fetchAll();
  
      $hash = $row[0]["password"];
  
      //$hash = $results[0]["password"];
  
          if ($password == $hash){
  
              if($newpassword == $confirmnewpassword) {
  
              $sql = "UPDATE users SET password = '$newpassword' WHERE    username  = '$username'";
  
              $query = $dbh-&gt;prepare($sql);
  
              $query-&gt;execute();
  
              echo "Password Changed Successfully!";
  
          } else echo "Passwords do not match!";
          }
      } else echo "Please type your current password accurately!";
  }
  ?&gt;
  </code></pre>
  
  <p>Jquery</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function() {
          var frm = $('#resetform');
          frm.submit(function(e){
              $.ajax({
                  type: frm.attr('method'),
                  url: frm.attr('action'),
                  data: frm.serialize(),
                  success: function(data){
                      $('#success').html("&lt;div id='message'&gt;&lt;/div&gt;");
                      $('#message').html("&lt;h2&gt;Password changed successfully!&lt;/h2&gt;")
                      .delay(3000).fadeOut(3000);
                  },
                  error: function(data) {
                      $('#error').html("&lt;div id='errorMessage'&gt;&lt;/div&gt;");
                      $('#errorMessage').html("&lt;h3&gt;Error, please try again!&lt;/h3&gt;")
                      .delay(2000).fadeOut(2000);
                  }
  
              });
  
              e.preventDefault();
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>i would appreciate any corrections :-)</p>