guttyguppy Posted September 5, 2008 Share Posted September 5, 2008 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 https://forums.phpfreaks.com/topic/122811-solved-need-ajax-to-write-in-a-random-word-to-a-text-input-when-a-link-is-clicked/ Share on other sites More sharing options...
barkermn01 Posted September 5, 2008 Share Posted September 5, 2008 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 https://forums.phpfreaks.com/topic/122811-solved-need-ajax-to-write-in-a-random-word-to-a-text-input-when-a-link-is-clicked/#findComment-634548 Share on other sites More sharing options...
guttyguppy Posted September 6, 2008 Author Share Posted September 6, 2008 Thank you so much, it worked perfectly! Link to comment https://forums.phpfreaks.com/topic/122811-solved-need-ajax-to-write-in-a-random-word-to-a-text-input-when-a-link-is-clicked/#findComment-635011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.