【发布时间】:2012-10-20 19:23:18
【问题描述】:
所以我正在尝试在我的网站上实现密码更改功能,并且我想提交密码表单而不刷新页面。所以我正在尝试使用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 输入的内容。如果它们匹配,则它会更改密码。
【问题讨论】:
-
也制作 $('#success_div').html(response); //正确更新DIV