Nothing has quite fit my needs here, Im sure this is easy compared to most things but I really have no knowledge or understanding of jQuery, so I am kind of flailing here.
I have a password change form (That currently works as far as changing the password goes) but what it doesnt do is show that anything has happened. So right now when I fill the passwords out, hit submit, the form is submitted to the changePassword.php script, and handled properly, but I get no visible indication of this.
I would like the password forms to clear, and a div underneath the button to populate with one of my $response messages.
main.php
<div id="s-window">
<form id="changepassword" action="changePassword.php" method="POST">
<input type="password" name="currentPassword" placeholder="Current Password"/>
<input type="password" name="newPassword" placeholder="New Password"/>
<input type="password" name="confirmPassword" placeholder="Confirm Password"/>
<input class="button" type="submit" value="Change Password" />
</form>
<div id="response"></div>
jQuery in main.php:
$(document).ready(function(){
$("#changepassword").submit(function(e) {
e.preventDefault(); // stop normal form submission
$.ajax({
url: "changePassword.php",
type: "POST",
data: $(this).serialize(), // you also need to send the form data
dataType: "html",
success: function(data){ // this happens after we get results
$("#results").show();
$("#results").append(data);
}
});
});
});
And finally, the script changePassword.php
$currentPassword = ($_POST['currentPassword']);
$password = ($_POST['newPassword']);
$password2 = ($_POST['confirmPassword']);
$username = ($_SESSION['username']);
$response = '';
if($password === '' || $password === FALSE){
$response = "Your password cannot be blank!";
} else {
if(strlen($password)<7){
$response = "Your password is too short!";
} else {
if ($password <> $password2) {
$response = "Your passwords do not match.";
}
else if ($password === $password2){
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$sql = "UPDATE Staff SET password='$hashed_password' WHERE username='$username'";
mysql_query($sql) or die( mysql_error() );
echo $response;
}
else { mysqli_error($con); }
};
};