Advanced Search

Results 1 to 4 of 4

Thread: AJAX Display on Screen

  1. #1
    Join Date
    Dec 2008
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default AJAX Display on Screen

    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

  2. #2
    Join Date
    Dec 2008
    Location
    Nigeria
    Posts
    97
    Thanks
    3
    Thanked 8 Times in 8 Posts

    Default

    index.html
    Code:
    <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
    Code:
    Hey, you did not give me no cd!!

  3. The Following User Says Thank You to diltony For This Useful Post:

    stephenalistoun (01-02-2009)

  4. #3
    Join Date
    Dec 2008
    Location
    Nigeria
    Posts
    97
    Thanks
    3
    Thanked 8 Times in 8 Posts

    Default Ajax Demo Help

    index.html
    Code:
    <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
    Code:
    Hey, you did not give me no cd!!

  5. The Following User Says Thank You to diltony For This Useful Post:

    stephenalistoun (01-02-2009)

  6. #4
    Join Date
    Dec 2008
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Cool

    Thanks diltony,

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


    Regards

    Stephen

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •