How to use header redirect in PHP AJAX Call In Pure Javascript? - Stack Overflow

I am creating a login form and I need to redirect the user to hisher profile page! I am using AJAX Req

I am creating a login form and I need to redirect the user to his/her profile page! I am using AJAX Requests so header redirect is not working at all. It just stays on the homepage.

So how can I redirect the user to another page in pure javascript PHP ajax call? Please give the answer in pure javascript. I don't like to use jQuery at all!

Javascript:

function ajaxCall(){
    var xhttp;

    if(window.XMLHttpRequest){
        xhttp = new XMLHttpRequest();
    }

    xhttp.onreadystatechange = function(){
        if(this.readyState === 4 && this.status === 200){
            document.getElementById('error').innerHTML = this.responseText;
        }
    };

    var parameters = 'email='+document.getElementById('email')+'&password='+document.getElementById('password');
    xhttp.open('POST', 'login.php', true);
    xhttp.setRequestHeader('Content-type', 'application/x-www/form/urlencoded');
    xhttp.send(parameters);
}

Login.php(PHP)

<?php
if(isset($_POST['email']) && isset($_POST['password'])){
    $ema = $_POST['email'];
    $pass = $_POST['password'];

    if(!empty($ema) && !empty($pass)){
        if($ema === 'Bill' && $pass === 'Cool'){
            header('Location: ');
        }
    }
}

I am creating a login form and I need to redirect the user to his/her profile page! I am using AJAX Requests so header redirect is not working at all. It just stays on the homepage.

So how can I redirect the user to another page in pure javascript PHP ajax call? Please give the answer in pure javascript. I don't like to use jQuery at all!

Javascript:

function ajaxCall(){
    var xhttp;

    if(window.XMLHttpRequest){
        xhttp = new XMLHttpRequest();
    }

    xhttp.onreadystatechange = function(){
        if(this.readyState === 4 && this.status === 200){
            document.getElementById('error').innerHTML = this.responseText;
        }
    };

    var parameters = 'email='+document.getElementById('email')+'&password='+document.getElementById('password');
    xhttp.open('POST', 'login.php', true);
    xhttp.setRequestHeader('Content-type', 'application/x-www/form/urlencoded');
    xhttp.send(parameters);
}

Login.php(PHP)

<?php
if(isset($_POST['email']) && isset($_POST['password'])){
    $ema = $_POST['email'];
    $pass = $_POST['password'];

    if(!empty($ema) && !empty($pass)){
        if($ema === 'Bill' && $pass === 'Cool'){
            header('Location: https://www.google.');
        }
    }
}
Share Improve this question edited Jul 6, 2017 at 12:42 Bill asked Jul 6, 2017 at 12:28 BillBill 711 silver badge9 bronze badges 9
  • 1 Can you show the code that you have written so far? – Bluefire Commented Jul 6, 2017 at 12:28
  • header('Location: profile.php') not working at all in AJAX! Is there a another way to redirect the user in pure javascript! – Bill Commented Jul 6, 2017 at 12:30
  • Aaah! OK! @Bluefire – Bill Commented Jul 6, 2017 at 12:30
  • Possible duplicate of How to redirect to another webpage in JavaScript/jQuery? – Abaddon666 Commented Jul 6, 2017 at 12:32
  • @Bill in what sense is it not working? Is there an error message? – Bluefire Commented Jul 6, 2017 at 12:34
 |  Show 4 more ments

3 Answers 3

Reset to default 3

Make an ajax call.

<?php
header('Content-Type: application/json');
echo json_encode(['location'=>'/user/profile/']);
exit;
?>

The ajax response will return something like

{'location':'/user/profile/'}

In your ajax success javascript function

 xhr.onreadystatechange = function () {
            if (xhr.status >= 200 && xhr.status <= 299)
            {
                var response = JSON.parse(xhr.responseText);
                if(response.location){
                  window.location.href = response.location;
                }
            } 

    }

I landed here looking for an Ajax solution, nevertheless MontrealDevOne's answer provided useful. If anyone else is curious about the ajax method too, here you go:

$('body').on('submit', '#form_register', function (e) {
    var form = $(this);
    $.ajax({
          type: 'POST',
          url: 'form_submissions.php',
          data: form.serialize() + "&submit",
          dataType:"json",
              success: function (data) {
              if(data.location){
                window.location.href = data.location;
              }
          },
          error: function(data) {
              alert("Error!");
          }
      });
    e.preventDefault();
});

Just my 2 cents, hope it helps :)

try this bill if its works.

  window.location.replace("http://www.google.");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744348882a4569857.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信