Need help with some ajax code

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Watch

Barrucadu

Established Member
Impact
64
Here is my code:
Code:
function Ajax(e) {
	/* Cancel the submit event, and find out which form was submitted */
	knackerEvent(e);

	document.getElementById('AJAX_RESULT').innerHTML = '<div align="center"><img src="../images/loader.gif" width="32" height="32" /><br />Searching. . .</div>';
	
	/* Set up the request */
	var xmlhttp =  new XMLHttpRequest();
	
	alert('Created XML Request Object.');
	
	xmlhttp.open('POST', 'search.php', true);
	
	alert('Opened Page.');
	
	/* Send the POST request */
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('q=' + document.getElementById('AJAX_FORM').elements['q'].value);
	
	alert('Sent Post Data.');
	
	/* The callback function */
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200){
				document.getElementById('AJAX_RESULT').innerHTML = xmlhttp.responseText;
			}else{
				document.getElementById('AJAX_RESULT').innerHTML = '<div align="center"><img src="../images/error.png" width="37" height="37" /><br /><span class="error">An error occured.</span></div>';
			}
		}else{
				document.getElementById('AJAX_RESULT').innerHTML = '<div align="center"><img src="../images/error.png" width="37" height="37" /><br /><span class="error">An error occured.</span></div>';
		}
	}
}

It sends the post data, but dosnt display the html generated by search.php, any ideas why?
 
Last edited:
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Try this on for size:

Code:
function Ajax() {
	knackerEvent(e);
	document.getElementById('AJAX_RESULT').innerHTML = '<div align="center"><img src="../images/loader.gif" width="32" height="32" /><br />Searching. . .</div>';
	
	http = GetXmlHttpObject();
	alert("Created XML Request Object.");

 	http.open("POST", "search.php", true);
	alert("Opened a Page With The POST Method.");
	
  	http.onreadystatechange = stateChanged;
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  	
	
	http.send("q=" + document.getElementById('AJAX_FORM').elements['q'].value);
	alert('Sent Post Data.');
}

function stateChanged()  { 
  if (http.readyState == 4 || http.readyState == "complete") { 
    if (http.status == 200) {
		document.getElementById("AJAX_RESULT").innerHTML = http.responseText 
	} else {
		document.getElementById('AJAX_RESULT').innerHTML = '<div align="center"><img src="../images/error.png" width="37" height="37" /><br /><span class="error">An error occured.</span></div>';
	}
  } else {
	document.getElementById('AJAX_RESULT').innerHTML = '<div align="center"><img src="../images/error.png" width="37" height="37" /><br /><span class="error">An error occured.</span></div>'; 
  }
}

function GetXmlHttpObject() { 
	var objXMLHttp = null
	if (window.XMLHttpRequest) { objXMLHttp = new XMLHttpRequest() } 
	else if (window.ActiveXObject) { objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP") }
	return objXMLHttp
}
 
1
Close
Thanks! rep added!

i'll have a good look at the code to get a better understanding of ajax
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

A browser tab with a favicon next to the page title
Search members
You can now search for members by name, company, location and more. Open the search at the top of any page and choose Members. You will...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back