所以我试图在我的网站上实现密码更改功能,我想在不刷新页面的情况下提交密码表单。所以我正在尝试使用ajax。这是我的html:
<form id="change_Pass" action="" method="post">
Current Password<input type="password" id="change_password" name="change_password"><br>
New Password<input type="password" id="new_password" name="new_password"><br>
Verify Password<input type="password" id="verify_password" name="verify_password"><br>
<input type="submit" value="Submit">
</form>
然后是jQuery:
$('#change_Pass').submit(function(e){
$.ajax({
data: $(this).serialize(), // get the form data
type: $(this).attr('POST'), // GET or POST
url: $(this).attr('Private/change_password.php'), // the file to call
success: function(response) { // on success..
$('#success_div).html(response); // update the DIV
},
error: function(e, x, r) { // on error..
$('#error_div).html(e); // update the DIV
}
});
e.preventDefault();
});
然后是php:
<?php
$usr = $_SESSION["username"];
$old_pwd = $_POST["change_password"];
$new_pwd = $_POST["new_password"];
$link = new PDO('mysql:host=*;dbname=*;charset=UTF-8','*','*');
$query = "SELECT *
FROM Conference
WHERE Username = :un";
$stmt = $link->prepare($query);
$stmt->bindParam(':un', $usr);
$stmt->execute();
$row = $stmt->fetchAll();
$hash = $row[0]["Password"];
$is_correct = Bcrypt::check($old_pwd, $hash);
if($is_correct) {
$query = "UPDATE Conference
SET `Password`=:new_pwd
WHERE Username = :usr";
$stmt = $link->prepare($query);
$stmt->bindParam(':new_pwd', $new_pwd);
$stmt->bindParam(':usr', $usr);
$stmt->execute();
}
但我被困在一些事情上。
1)如何将数据发布到 change_password.php 而不是序列化它以便我可以使用$_POST
?
2) change_password 看起来是否正确?它基本上是检查current password
用户使用数据库中现有密码输入的内容。如果它们匹配,那么它会更改密码。