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!

Link to comment
Share on other sites

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');

 

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.