rashmi_k28 Posted September 29, 2008 Share Posted September 29, 2008 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>'; ?> Quote Link to comment Share on other sites More sharing options...
nj Posted September 29, 2008 Share Posted September 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
rashmi_k28 Posted September 29, 2008 Author Share Posted September 29, 2008 No it is not working. When should I palce the echo '.$time_val.' The same code I tested but no change till I hit the refresh key Quote Link to comment Share on other sites More sharing options...
nj Posted September 29, 2008 Share Posted September 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
nj Posted September 29, 2008 Share Posted September 29, 2008 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; } } Quote Link to comment Share on other sites More sharing options...
rashmi_k28 Posted September 29, 2008 Author Share Posted September 29, 2008 Hi, No separating the client code and also php code the page doenot work. Can we not palce ajax and php code in the same file? 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.