2

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.com');
        }
    }
}
9
  • 1
    Can you show the code that you have written so far? 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! Commented Jul 6, 2017 at 12:30
  • Aaah! OK! @Bluefire Commented Jul 6, 2017 at 12:30
  • Possible duplicate of How to redirect to another webpage in JavaScript/jQuery? Commented Jul 6, 2017 at 12:32
  • @Bill in what sense is it not working? Is there an error message? Commented Jul 6, 2017 at 12:34

3 Answers 3

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;
                }
            } 

    }
Sign up to request clarification or add additional context in comments.

3 Comments

Isn't that success jQuery? If not how should i use in Javascript AJAX?
Thanks a lot bro! I just couldn't figure out your solution. But really thanks a lot! Marked as answer :)
What's the purpose of adding && xhr.status < 300 ? It will return false if it's not 200.
1

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 :)

1 Comment

this totally helped better understand the top answer - appreciate it bud
0

try this bill if its works.

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

4 Comments

No i am not talking about javascript redirect. I need PHP Header Redirect which is not working in AJAX Call!
@Bill try this once in your ajax code when you need to redirect if its works to redirect header.
Thanks for your answer bro! But got the good one from @MontrealDevOne
@Bill Roger Captan :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.