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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.