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'

    评论
  • weixin_33716557 2014-09-12 06:01
    关注

    JavaScript does not work with header() as it is browser-based language whereas PHP communicates directly with the Server. The best solution would probably be to return an error flag and message json_encode()'d.

    If you return 0 (error) then display a message.

    If you return 1 (success) redirect with JavaScript to a URL passed by php. That way you will be able to easily change the new URL should anything change in the website.

    JavaScript

    var frm = $('#frm_register');
        frm.submit(function (e)
        {
            e.preventDefault();
             $.ajax({
                type: frm.attr('method'),
                url: frm.attr('action'),
                dataType: 'json',
                data: frm.serialize(),
                success: function (data) {
                    if (data.r == 0){
                        $('#error').text(data.m);
                    }
                    if (data.r == 1){
                        document.location.href = data.m;
                    }
                }
            });
            return false;
        });
    

    PHP

    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)
         {
           echo json_encode(array(
               'r' => 1,
               'm' => 'anotherpage.php'
           ));
           exit();
         }
         else
         {
           echo json_encode(array(
               'r' => 0,
               'm' => 'Passwords do not match'
           ));
           exit();
         }
      }
    }
    
    评论
  • 狐狸.fox 2014-09-12 06:06
    关注
    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) {
                  if(data) {
                    winlow.location = data;
                  }
                }
            });
            return false;
        });
    

    In your action page just echo the link where you wanna redirect if you want

    评论
  • weixin_33709364 2014-09-12 06:07
    关注

    You can simply use javascript to redirect to the page like below:

    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)
         {
           echo true;
         }
         else
         {
            echo 'password does not match';
         }
      }
    }
    

    And for redirecting, you can use:

    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) {
                         if(data === true) {
                          window.location = 'Your url path here';
                         } else {
                          $('#error').text(data);
                        }
                }
            });
            return false;
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信
  • ¥15 使用QT实现can通信
  • ¥15 关于sp验证的一些东西,求告知如何解决,
  • ¥35 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题
  • ¥15 prophet运行报错,如何解决?
  • ¥15 用GPU跑pytorch搭建的LSTM的时候出现了奇怪的报错
  • ¥20 前端数据是从session等作用域拿到的,如何取值继续传递后端呢