DRock Posted April 25, 2009 Share Posted April 25, 2009 This one has been bugging me all night. I have used Ajax quite a bit before but I can't figure out why this doesn't work. The problem is at www.ymsrunning.com when you click the "Email Updates" link and enter an email in the form. The form calls the following JS function: function addEmail(form) { var email = form.email.value; if ((email.indexOf(".") > 2) && (email.indexOf("@") > 0)) { var url = "signup.php"; request.open("POST", url, true); request.onreadystatechange = updateEmail; request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send("email=" + email); } else alert("You did not enter a valid email address."); } function updateEmail() { if (request.readyState == 4) { if (request.status == 200) { var response = request.responseText; alert(response); } else alert("Error! Request status is " + request.status); } } The request variable exists up until the if statement checking for 200 status. If I check the status at that point it is always 0, but the variable is not null before that line. Once the line runs JS generates an error stating request is null. I am pulling my hair out with this one, and I tried running the PHP file on its own and it doesn't generate any errors. I would really appreciate any help. Thanks. Quote Link to comment Share on other sites More sharing options...
DRock Posted April 25, 2009 Author Share Posted April 25, 2009 A little sleep goes a long way. I was using a submit button which was refreshing my page, causing another server request to run at the same time. Changing the submit button to a normal button solved the problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.