Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
XMLHttpRequest gets url but can't redirect

I'm making a login form true an XMLHttpRequest. When a user enters incorrect details, a message will show up with an error. But when a user enters correct details, he should go to a new page (dashboard).

This is a part of my PHP

<?PHP 

// Details are correct
$output = header('Location:dashboard/index.php');

// Details are incorrect
$output = '<h6 class="omessagetext" id="error" style="color: #f05461">Incorrect e-mail or password</h6>';

?>

This is my javascript

var data = new FormData();
    data.append("email", document.getElementById("inputmail").value);
    data.append("password", document.getElementById("inputpassword").value);


var xhr = new XMLHttpRequest();
xhr.open("POST", "login.php");
xhr.onload = function() {
    
    if (xhr.readyState == 4 && xhr.status == 200) {
    
    var responsetext = xhr.responseText;
    
    if (responsetext.location){
        window.location.href = responsetext.location
    }else {
        document.getElementById('errordiv').innerHTML = responsetext;
    }
    
    }
};

xhr.send(data);

return false;

I've searched a lot but can't find the answer. Hope you can help me!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!