这是changepassword.php文件
<?php
include 'core/init.php';
if (empty($_POST) === false) {
if (md5($_POST['current_password']) === $user_data['password']) {
} else {
$errors[] = 'Your current psasword is incorrect';
}
}
if (empty($_POST) === false && empty($errors) === true) {
change_password($session_user_id, $_POST['password']);
} else if (empty($errors) === false) {
$error = output_errors($errors);
此变量
}
?>
这是jQuery文件
$('#save_pass').click(function(){
var current_password = $('#current').val();
var password = $('#new').val();
var password_again = $('#confirm').val();
if ((password == password_again) && (password.length >= 8)) {
$.post('changepassword.php', {current_password: current_password, password: password, password_again: password_again });
$('#show').html('password changed').fadeIn(500).delay(2000).fadeOut(500);
} else if ((current_password ==0) || (password_again.length == 0) || (password.length == 8)) {
$('#show').html('all fields are required').fadeIn(500).delay(2000).fadeOut(500);
} else {
$('#show').html('your password must be at least 8 characters').fadeIn(500).delay(2000).fadeOut(500); }
$('#current').val(null);
$('#new').val(null);
$('#confirm').val(null);
});
当用户输入错误密码并点击id =“#save_pass”的更改密码按钮时,我想回显$ error变量
答案 0 :(得分:0)
您无法在javascript文件中回显php变量。相反,将javascript放在你的php文件中并在那里回显 - 例如:
<script>
function something() {
alert('<?php echo $error; ?>');
}
</script>
答案 1 :(得分:0)
为了从$ .post()ajax调用中找回错误,您需要在PHP脚本中echo $errors
。然后在$.post()
:
$.post('changepassword.php', {current_password: current_password ...})
.done(function (data) {
alert(data);
}
这应该是获取原始回声数据的基础知识,但请查看$.post文档以获取有关如何优化此数据的更多指导。