csckid Posted April 7, 2009 Share Posted April 7, 2009 I tried the following code, it didn't give any output <html> <title>Untitled Document</title> <head> <script type="text/javascript"> var ajaxRequest; function getServerTime(){ ajaxRequest=getXMLHttpRequest(); if((!ajaxRequest){ document.getElementById("showtime").innerHTML="Request error"; return; } var myURL="telltime.php"; var myRand=parseInt(Math.random()*999999999); myURL=myURL+"?rand="+myRand; ajaxRequest.onreadystatechange=ajaxResponse; ajaxRequest.open("GET",myURL); ajaxRequest.send(null); } function getXMLHttpRequest(){ var xmlHttp,e; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("MSXML2.XMLHttp.6.0"); } catch (e) { try { xmlHttp=new ActiveXObject("MSXML2.XMLHttp.3.0"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } return xmlHttp; } function ajaxResponse(){ if(ajaxRequest.readyState!=4){return;} else{ if(ajaxRequest.status==200){ document.getElementById("showtime").innerHTML=ajaxRequest.responseText; } else{ alert("Request Failed"); } } } </script> </head> <body> <h2> This is ajax </h2> <form name="myform"> <input type="button" value="getTime" onClick="getServerTime()"> </form> <div id="showtime">Output</div> </body> </html> telltime.php <html> <head> </head> <body> <?php echo date('H:i:s'); ?> </body> </html> plz help Quote Link to comment Share on other sites More sharing options...
xtopolis Posted April 8, 2009 Share Posted April 8, 2009 function getServerTime(){ ajaxRequest=getXMLHttpRequest(); if((!ajaxRequest){ You have an extra ( in your if statement, it should read: if(!ajaxRequest){ Other than that it worked for me. Also in your telltime.php, you do not need the HTML in there, only the <?php echo date('H:i:s'); ?>. I only say this because you are returning extra information to your function that is unnecessary and will mess with formatting later. Tips for debugging your code: Use Firefox. Look in Firefox's Error Console (Tools->Error Console). It helps a lot. Gl. Quote Link to comment Share on other sites More sharing options...
csckid Posted April 8, 2009 Author Share Posted April 8, 2009 Thanks!!! that was a very good tips Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.