Padgoi Posted February 19, 2015 Share Posted February 19, 2015 I have this script that pulls post data from a database and displays it on my site. I need this to update in real time with Ajax whenever the topics table updates. I know literally nothing about Ajax. Can someone assist with this please? <?php //connect to the server $connect = mysql_connect("localhost","",""); //connect to the database mysql_select_db(""); //query the database $query = mysql_query("SELECT * FROM topics ORDER BY last_post"); //fetch the results / convert results into an array WHILE($rows = mysql_fetch_array($query)): $topic_id = $rows['last_post']; $topic_title = $rows['title']; $reply_author = $rows['last_poster_name']; $reply_time = date('h:i A', $rows['last_post']); $reply_date = date('l, F d, Y', $rows['last_post']); endwhile; echo "The last post was made in <font color='blue'>$topic_title</font> by <font color='red'>$reply_author</font> at <font color='green'>$reply_time</font> on <font color='brown'>$reply_date</font>."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/294735-how-can-i-make-this-script-refresh-with-ajax-whenever-a-table-updates/ Share on other sites More sharing options...
QuickOldCar Posted February 20, 2015 Share Posted February 20, 2015 The term to look for is ajax polling Quote Link to comment https://forums.phpfreaks.com/topic/294735-how-can-i-make-this-script-refresh-with-ajax-whenever-a-table-updates/#findComment-1506233 Share on other sites More sharing options...
Padgoi Posted February 20, 2015 Author Share Posted February 20, 2015 Thank you for literally not helping at all. Quote Link to comment https://forums.phpfreaks.com/topic/294735-how-can-i-make-this-script-refresh-with-ajax-whenever-a-table-updates/#findComment-1506267 Share on other sites More sharing options...
Psycho Posted February 20, 2015 Share Posted February 20, 2015 Padgoi, what specifically are you wanting? This forum is for people to get help with code they have written. It doesn't look like you've even made an attempt based on what you posted. A question such as how do I update a page with AJAX is not about helping to fix a problem in code or to make a change to existing code to get the expected results. It is about implementing a new technology into the existing process. A forum is not the appropriate medium for someone to ask others to team them how to do something new. What usually works is for the person to do a little research (e.g. tutorials) and attempt to implement the new feature. Then, if problems are experienced or if advice is needed, then make a post as to what has been done and what is not working as expected. QuickOldCar pointed you to a google results page that you should have done yourself to at least do some research before asking a question. So, here is my advice. 1. If you have not used JQuery, you will want to for this. It makes AJAX (and many other things with JavaScript) much easier. There will be a learning curve, but for what you need to do, there are only a handful of things you will need to learn initially. 2. Fix your current code. FONT tags? Those have been deprecated for over a decade! Use SPAN tags with style properties. That code should be it's own separate file if it isn't already. The page which will display that content should use a DIV to contain the content <div id="last_post"><?php include('last_post.php'); ?></div> 3. Create a timed event using JQuery that will fire every n seconds. That event will use the the JQuery ajax load event such as $( "#last_post" ).load( "last_post.php" ); That will work, but it is inefficient. You should pass a timestamp to the ajax call with the last time a request was made. That way the query to get the last post can determine if there is new content or not. If not then no update needs to be sent back. http://api.jquery.com/load/ Quote Link to comment https://forums.phpfreaks.com/topic/294735-how-can-i-make-this-script-refresh-with-ajax-whenever-a-table-updates/#findComment-1506270 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.