Jump to content

How can I make this script refresh with ajax whenever a table updates ?


Recommended Posts

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>.";
 
?>

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/

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.