weixin_33698043 2014-09-12 05:48 采纳率: 0%
浏览 31

使用AJAX在php中添加标头

My question is simple, I'm using AJAX and i want to redirect the user to another page if the user fill up the registration form properly, however if the user failed to match his/her password. i want to show an error message.

here is my PHP code:

if (isset($_POST['password']) && isset($_POST['retype_password']))
{
  $password = $_POST['password'];
  $retype_password = $_POST['retype_password'];
  if(!empty($password) && !empty($retype_password))
  {
     if($password == $retype_password)
     {
       header("Location: anotherpage.php");
       exit();
     }
     else
     {
        echo 'password does not match';
     }
  }
}

here is my ajax:

var frm = $('#frm_register');
    frm.submit(function (e)
    {
        e.preventDefault();
         $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                $('#error').text(data);
            }
        });
        return false;
    });

The problem here is that it doesn't redirect to another page unless i refresh the page.

  • 写回答

4条回答 默认 最新

  • larry*wei 2014-09-12 05:54
    关注

    Instead of header("Location: anotherpage.php"); just do echo '1' and in your AJAX call, if data['responseText'] == '1' than just do a document.location.href = 'anotherpage.php'

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器