PDA

View Full Version : AJAX Display on Screen



stephenalistoun
12-30-2008, 09:15 PM
Hi all,

I'm quite new to AJAX and I cannot display data from the file called getcd.php.

Could you guys please help me.


<script>
var xmlHttp

function GetXmlHttpObject()
{
var xmlHttp=null;

try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

function showCD()
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}

var url="checkUpdate.php"
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)

setTimeout("showCD()",1000);
}


function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
if(xmlHttp.responseText > 0)
{

/********I'm Stuck somewhere here*********/

xmlHttp2=GetXmlHttpObject()

if (xmlHttp2==null)
{
alert ("Browser does not support HTTP Request")
return
}

var url="getcd.php"
xmlHttp2.open("GET",url,true)
xmlHttp2.send(null)



document.getElementById("txtHint").innerHTML = xmlHttp2.responseText
}
}
}
</script>


Thanks

Stephen

diltony
12-31-2008, 12:01 AM
index.html


<html>
<head>
<title>Ajax Post</title>
<script>
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){try{return new ActiveXObject(activexmodes[i])} catch(e){alert("Failed");}}
} else if (window.XMLHttpRequest) {return new XMLHttpRequest()} else {return false}
return;
}

function loadURL(url) {
//to prevent browser from caching data
var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200){
processData(mygetrequest.responseText); //download successful
}
else{
alert('download failed.');
}
//ajax request would have been completed at this point
}
}
mygetrequest.open("POST", url+bustcacheparameter, true);
mygetrequest.send(null);
}

function processData(responseText) {
document.getElementById("txtHint").innerHTML=responseText;
}

function init() {
loadURL("getcd.php");
}
onload=init;
</script>
</head>
<body>
<div id="txtHint"></div>

</body>
</html>


getcd.php


Hey, you did not give me no cd!!

diltony
12-31-2008, 12:10 AM
index.html


<html>
<head>
<title>Ajax Post</title>
<script>
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){try{return new ActiveXObject(activexmodes[i])} catch(e){alert("Failed");}}
} else if (window.XMLHttpRequest) {return new XMLHttpRequest()} else {return false}
return;
}

function loadURL(url) {
//to prevent browser from caching data
var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200){
processData(mygetrequest.responseText); //download successful
}
else{
alert('download failed.');
}
//ajax request would have been completed at this point
}
}
mygetrequest.open("POST", url+bustcacheparameter, true);
mygetrequest.send(null);
}

function processData(responseText) {
document.getElementById("txtHint").innerHTML=responseText;
}

function init() {
loadURL("getcd.php");
}
onload=init;
</script>
</head>
<body>
<div id="txtHint"></div>

</body>
</html>


getcd.php


Hey, you did not give me no cd!!

stephenalistoun
01-02-2009, 08:23 AM
Thanks diltony,

I got the code working and your example of AJAX was do much better.
:cool:

Regards

Stephen