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
https://forums.phpfreaks.com/topic/116901-strange-xmlhttprequest-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.