使用ajax刷新修改密码,AJAX密码更改

,所以我想实现密码更改功能,我的网站,我想提交密码表单不刷新页面。所以我试图使用ajax。这里是我的html:

AJAX密码更改

Current Password

New Password

Verify Password

然后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:

$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。如果它们匹配,则它会更改密码。

2012-10-20

Richard

+0

也使$( #success_div)HTML(响应)。 //更新DIV正确 –

,所以我想实现密码更改功能,我的网站,我想提交密码表单不刷新页面。所以我试图使用ajax。这里是我的html: AJAX密码更改 Current Password New Password Verify Password 然后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: $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。如果它们匹配,则它会更改密码。 2012-10-20 Richard +0 也使$( #success_div)HTML(响应)。 //更新DIV正确 –
经验分享 程序员 微信小程序 职场和发展