Jump to content

[SOLVED] ajax begineer


csckid

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/153027-solved-ajax-begineer/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/153027-solved-ajax-begineer/#findComment-804118
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.