I have an Ajax script that allows me to check a form without refreshing the page. But i want it to check if fields haven’t been complete, but if al fields are complete i want to send the user to a new page.
I’m not sure how i can do this. Here is the Ajax script:
function pass()
{
// Real Browsers (chrome)
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//Store data
var email = document.getElementById('email').value;
var firstname = document.getElementById('firstname').value;
var surname = document.getElementById('surname').value;
var address1 = document.getElementById('address1').value;
var address2 = document.getElementById('address2').value;
var postcode = document.getElementById('postcode').value;
//Open POST location
xhr.open("POST","addUserSeminar.php");
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
//Make object to store all data
var obj = {email: email, firstname: firstname, surname: surname, address1: address1, address2: address2, postcode: postcode};
//Encode the data with JSON and send it to the server
xhr.send("data=" + JSON.stringify(obj));
//Check return state and change "myDiv"
xhr.onreadystatechange=function()
{
if (xhr.readyState==4)
{
document.getElementById("settingsDiv").innerHTML = xhr.responseText;
}
}
//Return false so form doesn't submit
return false;
}
It sends the data out to a php file where i check if the boxes are filled, if not i each a message like “Name not found”. But if all data is posted then i add a record to my database and then return true.
How could i pick up that return in my Ajax script so i can test if its true, if so forward to a new page.
Thanks for the time.
return the string “TRUE” from server side on success
PHP CODE
client side JS code