尝试让这个脚本调用php。当我使密码不相等时,错误吐出密码不匹配,如果我把它留空,我也得到正确的错误信息。但是,如果我尝试正常使用它,我会得到未知的错误。此代码中是否存在无法匹配的操作顺序。我看不出有什么不对。
<script>
function changepass() {
var u = _("username").value;
var cp = _("currentPass").value;
var np = _("newPass").value;
var cnp = _("confirmNewPass").value;
if(np != cnp) {
_("status").innerHTML = "The passwords given do not match!";
} else if (cp === "" || np === "" || cnp === "") {
_("status").innerHTML = "Please fill out all of the fields.";
} else {
_("changepassbtn").style.display = "none";
_("status").innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "reset_pass.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
var response = ajax.responseText;
if(response.trim() == "success"){
_("status").innerHTML = 'Your password change was successful!';
} else if (response == "no_exist"){
_("status").innerHTML = "Your current password was entered incorrectly.";
_("changepassbtn").style.display = "initial";
} else if(response == "pass_failed"){
_("status").innerHTML = "Change password function failed to execute!";
_("changepassbtn").style.display = "initial";
} else {
_("status").innerHTML = "An unknown error occurred";
_("changepassbtn").style.display = "initial";
}
}
}
ajax.send("u="+u+"&cp="+cp+"&np="+np+"&cnp"+cnp);
}
}
</script>
这是php。
<?php
// AJAX CALLS THIS CODE TO EXECUTE
if(isset($_POST['cp'])) {
include_once("php_includes/db_conx.php");
$username = '';
$oldpasshash = '';
$newpasshash = '';
$u = mysqli_real_escape_string($db_conx, $_POST['u']);
$oldpass = $_POST["cp"];
$newpass = $_POST["cnp"];
$oldpasshash = md5($oldpass);
$newpasshash = md5($newpass);
$sql = "SELECT username, password FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$db_username = $row["0"];
$db_password = $row["1"];
if($db_password != $oldpasshash){
echo "no_exist";
exit();
} else {
$sql = "UPDATE users SET password='$newpashhash' WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$sql = "SELECT password FROM users WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$db_newpass = $row[0];
if($db_newpass == $newpasshash) {
echo "success";
exit();
} else {
echo "pass_failed";
exit();
}
}
}
?>
我也希望杀死所有用户cookie,但我有我的登出页面中的代码,但我不确定将它放在这个页面的哪个位置。任何建议都是好的。注意:我是一个新手,现在只是关注教程,我希望稍后在我理解它时更改它。
答案 0 :(得分:0)
您需要在您的网站中添加jquery的ajax库并使用下面的代码,您可以根据需要更改此代码
function checkpass() {
var password = $("#newpassword").val();
password = password.trim();
var confirmpassword = $("#confirmpassword").val();
confirmpassword = confirmpassword.trim();
var status = false;
if(password !== '' && confirmpassword !== ''){
if (password != confirmpassword) {
$("#newpassword").css('border', '1px solid red');
$("#confirmpassword").css('border', '1px solid red');
$('#password_error').html('Password and Confirm Password does not match!');
} else {
$("#newpassword").removeAttr('style');
$("#confirmpassword").removeAttr('style');
$('#password_error').html('');
status = true;
}
return status;
}
}
function changepass(){
var checkin = checkpass();
var url = 'myurl'; // change according to your need
if(checkin == true){
var newpass = $("#newpassword").val();
$.ajax({
url: url,
type: "POST",
data: $("#passowrdform").serialize(),
success: function (response){
//alert(response);
$('#old_pass').val(newpass)
$("#newpassword").val('');
$("#confirmpassword").val('');
}, error: function(){
}
});
}
return false;
}