Jump to content

[SOLVED] Server Request Becoming Null


DRock

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.