Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    No Response from Ajax

    So I've been at this ajax problem for a month now, which should show you how new I am to it. Anyways, everything looks as right as all the ajax tutorials out there say it should be but for some reason it doesn't give me a response. I've simplified the response to a date now as to show how nothing gets passed back to the frontpage. Here's the scripts:

    Javascript:
    Code:
    var xmlHttp;
    	
    function createXMLHttpRequest(){
    	var ajaxRequest;  // The variable that makes Ajax possible!	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	}catch(e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    		}catch(e){
    			try{
    				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			}catch(e){
    				// Something went wrong
    				alert ("Browser does not support HTTP Request");
    				return false;
    			}
    		}
    	}
    	
    	return ajaxRequest;
    }
    function eventHorizon(){
    	document.getElementById("cont").innerHTML="Starting...";
    	xmlHttp=createXMLHttpRequest();
    		var url="ajax-supp.php";
    	xmlHttp.onreadystatechange=callback
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    }
    function callback(){ 
    	if (xmlHttp.readyState==4){ 
    		if(xmlHttp.status==200){
    			document.getElementById("cont").innerHTML=xmlHttp.responseText;
    		}else{
    			document.getElementById("cont").innerHTML="Cannot find page: " + xmlHttp.status;
    		}
    	}else{
    		document.getElementById("cont").innerHTML="Waiting for server...";
    	}
    }
    HTML:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="pragma" content="no-cache" />
    <meta http-equiv="cache-control" content="no-cache" />
    <title>Ajax Test</title>
    <script ltype="text/javascript" src="ajax.js"></script>
    </head>
    <body>
    <div id="cont" style="border:#000000 solid thin;">
    hello
    </div>
    <br />
    <a href="" onclick="eventHorizon()">-Show</a>
    </body>
    </html>
    and the PHP:
    PHP Code:
    <?php
    echo date("H:i:s"); 
    ?>
    Please let me know what's wrong with this.

    Edit: here's a link if you want to try it out yourself.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by bilischan88 View Post
    Code:
    <a href="" onclick="eventHorizon()">-Show</a>
    the AJAX call is actually working but before it can return the page reloads because of the anchor tag

    u can use either
    Code:
    <a onclick="eventHorizon(); return false;" href="">-Show</a>
    or
    Code:
    <a onclick="eventHorizon()" href="#">-Show</a>
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you. I feel like an idiot now, but seriously thanks it works


  •  

    Posting Permissions

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