This question already has an answer here:
Okay so I am trying to get the PHP to save the users data to the XML and then forward the user onto the homepage..but when the JavaScript is ran it does not grab the echo's at the end of the script along with the header();
The PHP i wish to echo
echo "<b>Congratulations registration was Successful.<br /> Your Customer ID is: $id4</b>";
echo "<br/>";
echo "redirected automatically in 7 seconds";
header( "Refresh:7; buyonline.htm", true, 303);
$doc->save("data/customer.xml");
}
And the JavaScript function
var firstname = document.getElementById("firstName").value;
var lastname = document.getElementById("lastName").value;
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var password2 = document.getElementById("password2").value;
var number = document.getElementById("pNumber").value;
var type = "";
var input = document.getElementsByTagName("input");
xHRObject.open("GET", "testregristation.php?firstName=" + firstname + "&lastName=" + lastname + "&email=" + email + "&password="+password+"&pNumber="+number, true);
xHRObject.onreadystatechange = function() {
if (xHRObject.readyState == 4 && xHRObject.status == 200)
{
document.getElementById('information').innerHTML = xHRObject.responseText;
}
xHRObject.send(null);
Obviously these are small snippets but i do not understand why it wont send the response to the screen it shows the response in "firebug".. my PHP also does a email check to make sure its not in use, and that echoes perfectly.
</div>
I haven't really tested your code to make sure that what I'm about to say is 100% right. I never tried to use header in a way like this inside ajax response. But, I guess that if you want to redirect the user just use javascript for that.
PHP:
echo "<b>Congratulations registration was Successful.<br /> Your Customer ID is: $id4</b>";
echo "<br/>";
echo "redirected automatically in 7 seconds";
$doc->save("data/customer.xml");
Javascript:
if (xHRObject.readyState == 4 && xHRObject.status == 200)
{
document.getElementById('information').innerHTML = xHRObject.responseText;
setTimeout(function()
{
window.location.pathname = "/buyonline.htm";
}, 7000 /* 7 seconds */);
}
Edit: Now because you using javascript to redirect the page, you could also countdown the seconds:
PHP:
echo "<b>Congratulations registration was Successful.<br /> Your Customer ID is: $id4</b>";
echo "<br/>";
echo "redirected automatically in <span id="redirect-countdown">7 seconds</span>";
$doc->save("data/customer.xml");
Javascript:
if (xHRObject.readyState == 4 && xHRObject.status == 200)
{
document.getElementById('information').innerHTML = xHRObject.responseText;
var countdown = document.getElementById("redirect-countdown");
var count = 8;
var interval = setInterval(function()
{
count--;
countdown.innerText = count + " second" + (count > 1 ? "s" : "");
if(count <= 0)
{
clearInterval(interval); // Kill the interval timer
window.location.pathname = "/buyonline.htm"; // Redirect to that page
}
}, 1000);
}