0

这是 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 变量

4

2 回答 2

0

您不能在 javascript 文件中回显 php 变量。相反,将 javascript 放入您的 php 文件并在其中回显它 - 例如:

<script>
function something() {
    alert('<?php echo $error; ?>');
}
</script>
于 2013-09-01T22:09:22.317 回答
0

为了从您的 $.post() ajax 调用中取回错误,您需要echo $errors在您的 php 脚本中。然后将成功/完成功能添加到您的$.post()

$.post('changepassword.php', {current_password: current_password ...})
    .done(function (data) {     
        alert(data);
    }

这应该是获取原始回波数据的基础知识,但请查看$.post文档以获取有关如何完善它的更多指导。

于 2013-09-01T22:13:18.057 回答