Jump to content

reload


rashmi_k28

Recommended Posts

Hi,

 

The file I have named as time.php and id is 'time'.

 

Names is the function which is queried from the database Mysql.

When the database record changes I have to change the $time_val in time.php without hitting the refresh key.

 

The page has to dynamically change when the value gets changed in the database.

 

 

Please help me how can I do it using ajax

 

<?
$name       = Names(1);
array_shift($name);
$time_val       = $name[0];
?>

<html>

<head>
<script type="text/javascript">function getHTTPObject() {
        var xmlhttp;
        if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
                try{
                        xmlhttp = new XMLHttpRequest();
                } catch (e){
                        xmlhttp = false;
        }
        }
        return xmlhttp;
}
var http = getHTTPObject();

function changeDiv(){
        var url = "time.php";
        http.open("GET", url, true);
        http.send(null);
        document.getElementById('time').innerHTML = http.responseText;</script>
</head>
<?php
echo '<body  onLoad="window.setInterval("changeDiv()",1);">

<div id="time">'.$time_val.'</div>
</body>
</html>';
?>

Link to comment
Share on other sites

Hi rashmi_k28 ,

 

Am new in php, but am good at ajax,

 

Try this,

 

Save this code as a new php page and call it time.php or any other name.

 

<?

$name      = Names(1);

array_shift($name);

$time_val      = $name[0];

 

// make sure u echo $time_val

echo $time_val;

?>

 

now, do this to the page that is viewed by the client

 

<html>

 

<head>

<script type="text/javascript">

 

var xmlhttp;

 

  function getHTTPObject() {

        xmlhttp = null;

      try{

          xmlhttp = new XMLHttpRequest();

      }

      catch(e){

        try{

            xmlhttp = new ActiveXObject('Msxml.XMLHTTP');

        }

        catch(e){

            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');

      }

      }

        return xmlhttp;

}

 

function changeDiv(){

      xmlhttp = getHTTPObject();

      if(xmlhttp == null){

        alert('Browser does not support HTTP Requests');

        return;

      }

 

        var url = "time.php";

        xmlhttp.onreadystatechange = displayTime;

        xmlhttp.open("GET", url, true);

        xmlhttp.send(null);

}

function displayTime(){

      if(xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete'){

          document.getElementById('time').innerHTML = http.responseText;

      }

}

 

function timing(){

      window.setInterval("changeDiv()",60000);

}

 

 

</script>

</head>

<body onload="timing()">

<div id="time"></div>

</body>

 

Try it.

Link to comment
Share on other sites

 

The reason why it doesn't working is one, may be the php code is in the same page with the client page.

This two must be sepated as in the php code should be named time.php and the client page say client.php.

let this two be on the same folder.

Two, try writting a simple code in php for testing before getting to the real thing.

like

 

<?php

$time = date();

 

echo $time;

?>

 

save this as time.php and test with it. if it works check your php code.

Link to comment
Share on other sites

 

I have reviewed my code,

 

also check this function:-

 

function displayTime(){

      if(xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete'){

          document.getElementById('time').innerHTML = http.responseText;

      }

}

 

replace it with

 

function displayTime(){

      if(xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete'){

          document.getElementById('time').innerHTML = xmlhttp.responseText;

      }

}

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.