Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 1, 2026.

Need help with some ajax code

SpaceshipSpaceship
NamecheapNamecheap
SpaceshipSpaceship
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
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains — AI StorefrontUnstoppable Domains — AI Storefront
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
•••
Thanks! rep added!

i'll have a good look at the code to get a better understanding of ajax
 
0
•••
CatchedCatched

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back