Jump to content

[SOLVED] Need ajax to write in a random word to a text input when a link is clicked


guttyguppy

Recommended Posts

I'm trying to get a link on a page to insert a random word (presumably from some English dictionary) into a text field. I've written php to display a random word on a page whenever the page is refreshed, so I can probably build from that. I just don't know much about ajax or how to pull this off. Any help would be greatly appreciated, thanks!

Easy

function GetXmlHttpObject(){
var xmlHttp=null;
try{
		xmlHttp=new XMLHttpRequest();
}
catch (e){
	//Internet Explorer
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}
return xmlHttp;
}
function getPage(url){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
}
url=url+"?sid="+Math.random(); // fix to keep IE from cacheing the page

xmlHttp.open("GET",url,true);

xmlHttp.onreadystatechange=function() {
	if (xmlHttp.readyState == 4){
		document.getElementById("").value = xmlHttp.responseText; // The ID of your Text box
	}else{
		// Your Code to do when you dont have your page loaded so a nice laoding image works
	} 
}

xmlHttp.send(null);
}

Then onClick of your button

getPage('random.php');

 

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.