undertaker Posted March 19, 2011 Share Posted March 19, 2011 Does anyone know how to make that reading from database is live, without refreshing the site. Like new facebook comments. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/231106-mysql-live-update/ Share on other sites More sharing options...
Pikachu2000 Posted March 19, 2011 Share Posted March 19, 2011 Moving thread to AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/231106-mysql-live-update/#findComment-1189598 Share on other sites More sharing options...
phpmady Posted March 20, 2011 Share Posted March 20, 2011 Hi, Make a index.php, <html> <head> <script type="text/javascript"> function Ajax(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari } catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer xmlHttp = null; } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("No AJAX!?"); return false; } } } xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4){ document.getElementById('content').innerHTML=xmlHttp.responseText; xmlHttp = null; setTimeout('Ajax()',1000); } } xmlHttp.open("GET","data.php' ); xmlHttp.send(null); } window.onload=function(){ setTimeout('Ajax()',1000); } </script> </head> <body> Hi this is home page </body> </html> If you observe the above code, javascript have called the data.php file, so that u can live value from the database, without refreshing //data.php is called in the interval of 1 second, so u wil get the updated values from the database //ajax is called with the interval of 1 second in the div panel name "content" <?php echo "<div id="content">"; //select query //echo "column"; echo "</div>"; ?> All the best Quote Link to comment https://forums.phpfreaks.com/topic/231106-mysql-live-update/#findComment-1189838 Share on other sites More sharing options...
undertaker Posted March 20, 2011 Author Share Posted March 20, 2011 Thank you very much for your help Quote Link to comment https://forums.phpfreaks.com/topic/231106-mysql-live-update/#findComment-1189879 Share on other sites More sharing options...
soltek Posted April 14, 2011 Share Posted April 14, 2011 Hmm... I just copy pasted the index, added the content div and the data file but I got nothing good. What did I do wrong? <html> <head> <script type="text/javascript"> function Ajax(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari } catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer xmlHttp = null; } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("No AJAX!?"); return false; } } } xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4){ document.getElementById('content').innerHTML=xmlHttp.responseText; xmlHttp = null; setTimeout('Ajax()',1000); } } xmlHttp.open("GET","data.php' ); xmlHttp.send(null); } window.onload=function(){ setTimeout('Ajax()',1000); } </script> </head> <body> Hi this is home page <div id=content> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/231106-mysql-live-update/#findComment-1201715 Share on other sites More sharing options...
karimlo Posted May 1, 2012 Share Posted May 1, 2012 Hi PHPMady, I wanted to thank you for your post even if it's from long time ago... But this one helped me a lot and it worked fine. We just want to make sure to note that the last PHP piece of code can be inserted in the index.php page right underneath the "Hi, this is homepage" line. Also, the data.php is another file that users should already have to have with their Database connection code and the query that pulls the data from the mysql. Now, this is a working example that is great. I just need help with a multi-liner instead of one line. What if I have 50 records and I want to refresh the whole display table instead of only one record? What code do I need and where do I need it? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/231106-mysql-live-update/#findComment-1341926 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.