I am just planning a home page, and I did hit a problem. There are many class in the page, and I'm using AJAX for one of them to send / recieve informations from the server in every seconds.
The main reason is for using a different div for this, I don't want to refresh the whole home page, because it would be unuseable.
What I want to do with this div, when there is a little changing in the database, I want to reload the whole homepage automatically, to let that handle the changing, but I cannot control this from that div.
I tried the javascript method refresh(), and also I tried the HTML refresh meta-tag, without any fortune (the html tag worked only in chrome, but not in firefox)
I want to ask about is there any way to the page to reload when it changes (because ajax that div in the page is always changing)?
The following code was written on the fly, right into the post, so I make no promises that it works "as is". However, the concept is there, so it should still be semi-useful. It is also dependent on the JavaScript library jQuery, so if that isn't included, it won't work.
Basically what this does, is compare the post title contained in the <h2> with the one stored in the database. If the titles are different, then the PHP code sends updated data to the JavaScript, which populates the existing <div> with the updated information. Again, not really the best way, but I'm just trying to show a concept here.
HTML Code:
<div id="current_info"><h2 class="post_title">Post Title</h2><p>Couple random paragraphs #1</p><p>Couple random paragraphs #2</p><p>Couple random paragraphs #3</p><p>Couple random paragraphs #4</p></div>
A javascript BBC would be really useful right about now.
Code:
$(document).ready(function()
{
// setInterval repeats the provided function until clearInterval() is called.
setInterval(function()
{
// jQuery AJAX FTW!
$.ajax({
url: 'process.php',
type: 'post',
data: {oldTitle: $('#current_info h2.post_title').text()},
dataType: 'json',
success: function(response)
{
// If there's nothing coming back, then something is wrong.
if (!response)
return false;
// If the post is new (updated) keep going.
// response.status will be 'current' if the post is not updated.
if (response.status == 'updated')
{
// Clear the old post.
$('#current_info').html('');
// Add the title.
$('#current_info').append('<h2>' + response.title + '</h2>');
// Add the post content.
$('#current_info').append(response.post);
// Success!
return true;
}
// This would most likely be the response 'current'.
else
return false;
}
});
}, 10000); // Repeat this every ten seconds.
});
This is 'process.php', assumed to be in the same directory as the html. You can change the name of the script and its location here $.ajax({ url: })
PHP Code:
<?php
// Get some stuff from the database, and make sure it's sanitized!
$result = $database_result;
// The post displayed on the page matches the one in the database. Forget it.
if ($_POST['oldTitle'] == $result['title'])
$updated['status'] = 'current';
// Format the return data.
$updated = array(
'status' => 'updated',
'title' => $result['title'],
'post' => $result['post']
);
// Send it back to the JavaScript.
echo json_encode($updated);
Bookmarks