我正在尝试使用ajax创建重置密码模块,但php文件中的返回值(console.log()
)始终为空。我的php文件中也没有错误,我甚至不回显空字符串。请帮忙,这是我的ajax请求
$.ajax({
type:'POST',
url:'verify-change-pass',
data: {_oldpass : op, _pass1 : p1, _pass2 : p2},
success:function(data) {
data = data.trim();
switch(data){
case "er_no_acc" :
$('#grp_oldpass').addClass('has-error');
$('#feedback_oldpass').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_oldpass').innerHTML = "*No account found. Please refresh the page to generate new session.";
//alert('Please enter your username');
break;
case "er_oldpass_empty" :
$('#grp_oldpass').addClass('has-error');
$('#feedback_oldpass').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_oldpass').innerHTML = "*Please enter old password.";
// alert('Please enter your password');
break;
case "er_oldpass_invalid" :
$('#grp_oldpass').addClass('has-error');
$('#feedback_oldpass').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_oldpass').innerHTML = "*Old password does not match.";
// alert('Please enter your password');
break;
case "er_pass1_empty" :
$('#grp_pass1').addClass('has-error');
$('#feedback_pass1').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Please enter password.";
// alert('Please enter your password');
break;
case "er_pass2_empty" :
$('#grp_pass2').addClass('has-error');
$('#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass2').innerHTML = "*Please enter password.";
// alert('Please enter your password');
break;
case "er_pass1_tooshort" :
$('#grp_pass1').addClass('has-error');
$('#feedback_pass1').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Password must be more than 5 characters.";
// alert('Password too short.');
break;
case "er_pass2_tooshort" :
$('#grp_pass2').addClass('has-error');
$('#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass2').innerHTML = "*Password must be more than 5 characters.";
// alert('Password too short.');
break;
case "er_pass1_toolong" :
$('#grp_pass1').addClass('has-error');
$('#feedback_pass1').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Password must be less than 20 characters.";
// alert('Password too long.');
break;
case "er_pass2_toolong" :
$('#grp_pass2').addClass('has-error');
$('#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass2').innerHTML = "*Password must be less than 20 characters.";
// alert('Password too long.');
break;
case "er_pass_notsame" :
$('#grp_pass1,#grp_pass2').addClass('has-error');
$('#feedback_pass1,#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Password does not match.";
document.getElementById('msg_pass2').innerHTML = "*Password does not match.";
// alert('Password does not match');
break;
case "y" :
ResetMyPassword(p1);
break;
default:
alert('Error occured : '+data);
}
/* Always put console.log for debugging */
console.log(data);
}
});
这是我的verify-change-pass.php
session_start();
if(!isset($_SESSION['username'])){
echo "Error 404 : Page not found.";
exit();
}
include("connectdb.php");
$par_uname = $_SESSION['username'];
$par_oldpass = isset($_POST['_oldpass']) ? $_POST['_oldpass'] : '';
$par_pass1 = isset($_POST['_pass1']) ? $_POST['_pass1'] : '';
$par_pass2 = isset($_POST['_pass2']) ? $_POST['_pass2'] : '';
trim($par_oldpass);
trim($par_pass1);
trim($par_pass2);
htmlspecialchars($par_oldpass);
htmlspecialchars($par_pass1);
htmlspecialchars($par_pass2);
$count=0;
$stmt = $mysqli->prepare("SELECT acc_hashedpass FROM admin_acc WHERE acc_uname=?");
$stmt->bind_param('s',$par_uname);
$stmt->execute();
$stmt->store_result();
$count = $stmt->num_rows;
$stmt->bind_result($hashed);
$stmt->fetch();
$stmt->close();
if($count != 1){
echo "er_no_acc"; //no username found. check the session if fetched properly
exit();
}
if ($par_oldpass == ""){
echo "er_oldpass_empty"; //oldpass is empty
exit();
}
if(!password_verify($par_oldpass,$hashed)){
echo "er_oldpass_invalid" . password_verify($par_oldpass,$hashed); //invalid old password
exit();
}
if ($par_pass1 == ""){
echo "er_pass1_empty"; //password 1 is empty
exit();
}
if($par_pass2 == ""){
echo "er_pass2_empty"; //password 2 is empty
exit();
}
if(mb_strlen($par_pass1) < 6){
echo "er_pass1_tooshort"; //password is too short. Must be more than 8 characters
exit();
}
if(mb_strlen($par_pass2) < 6){
echo "er_pass2_tooshort"; //password is too short. Must be more than 8 characters
exit();
}
if(mb_strlen($par_pass1) > 20){
echo "er_pass1_toolong"; //password is too long. Must be less than 20 characters
exit();
}
if(mb_strlen($par_pass2) > 20){
echo "er_pass2_toolong"; //password is too long. Must be less than 20 characters
exit();
}
if ($par_pass1 != $par_pass2){
echo "er_pass_notsame"; //password doesnt match
exit();
}
echo "y";
来自verify-change-pass.php
的第一个到第6个if
的回声正在起作用,之后,返回值始终为空字符串。请帮忙..
顺便说一句,此代码段适用于localhost
,但在我上传后,代码无效。
答案 0 :(得分:0)
显然我需要+50声望才能发表评论,所以让我通过回答发表评论(笑)
error_reporting(E_ALL);
ini_set('display_errors', 1);
尝试将其添加到代码顶部?
另外,检查是否
include("connectdb.php");
内部有任何错误/可能退出。
您是否尝试过在最顶端回复某些内容?
答案 1 :(得分:0)
尝试从ajax调用的php文件代码中删除exit函数 并打印您想要的消息,并尝试提醒您在ajax成功获得的响应。 - &GT;你可以试试jQuery.trim(数据);而不是data.trim(); 希望这样可行。 - &GT;如果在php的ajax文件中,则尝试其他个人而不是个人