Jump to content

strange XMLHTTPrequest problem


Recommended Posts

Hi there i'm having a very strange problem when trying to use xmlhttprequest.  I create my new xmlhttprequest and all is fine and i send my values from a form to a php file where the back end work is done.

 

here's the code that takes from the form.

//run when shout form is submitted
function submitShout(){
httpObject = getHTTPObject();

name = getElement("nameID");
city = getElement("cityID");
shout = getElement("shoutID");
submitted = getElement("submitID");

namesubmit = encodeURIComponent(name.value);
citysubmit = encodeURIComponent(city.value);
shoutsubmit = encodeURIComponent(shout.value);

if (httpObject != null) {
	httpObject.open("GET", "shoutupdate.php?submit="+submitted.value+"&shouter="+namesubmit+"&city="+citysubmit+"&shout="+shoutsubmit, true);
	alert("just press ok");
	httpObject.send(null);
	alert("we'll fix this issue soon");
	httpObject.onreadystatechange = setOutput("outputID");
}

document.form.reset();
}

 

code to get xmlhttprequest

 

// Get the HTTP Object
function getHTTPObject(){
var xhr; 
    try {  
	xhr = new ActiveXObject('Msxml2.XMLHTTP');
	return xhr;
}
    catch (e) 
    {
        try {   
		xhr = new ActiveXObject('Microsoft.XMLHTTP');
		return xhr;
	}
        catch (e2) 
        {
          try {  
	  
	  	xhr = new XMLHttpRequest();
	  	return xhr;
	  }
          catch (e3) {
		  xhr = false;
			return xhr;
		}
        }
     }
}

 

 

the code to set my output back to the user

//set output span of given page
function setOutput(id){

var output = getElement(id);
if(httpObject.readyState == 4){

	output.innerHTML = httpObject.responseText;

}
else if(httpObject.readyState == 0){
	output.innerHTML = "ready state = 0";
}
else if(httpObject.readyState == 1){
	output.innerHTML = "ready state = 1";
}
else if(httpObject.readyState == 2){
	output.innerHTML = "ready state = 2";
}
else if(httpObject.readyState == 3){
	output.innerHTML = "ready state = 3";
}
else{
	output.innerHTML = "<img src=\"images/waiting.gif\"/>";	
}
}

 

and here's a bit of code that just lets me get the element by ID

function getElement(id){
if (document.getElementById){
	// this is the way the standards work
	var el = document.getElementById(id);
}else if (document.all){
	// this is the way old msie versions work
	var  el = document.all[id];
}else if (document.layers){
	// this is the way nn4 works
	var el = document.layers[id];
}
return el;

}

 

 

My problems lies with in submitShout().  Currently there are two alerts in there just to make the user press ok.  with these alerts in the code i successfully reach a readystate of 4.  When i remove those alerts, i get stuck at a readystate of 1, which means the send was not called.  the whole process works no matter what though, the values go to the php file and they go to the database,  just for some reason, without these alerts, it doesn't work.  This happens on all my AJAX functions which are of similar nature.

 

This is a very annoying problem and i don't understand why Alerts affect the code like that.  any help to resolve this problem is greatly greatly apreciated!  thanks for taking the time to read this.

 

-grant

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.