jquery refresh php loop?
I have a page in which users can make "posts" its similar to facebook, I am trying to figure out how I can get it to run the php loop say every 10mins, in order for the person to see new posts. Everytime a post is made it is added into the db and then the page is refreshed, I want to do it more "facebook like". Using jquery slide down etc. Below is what I have up2 now.
function postdata()
{
$.ajax({
type: "POST",
dataType: "text",
url: "makepost.php",
data: "post_text=" + $("#post_text").val(),
cache: false,
success: function(reply_text)
{
if (reply_text.indexOf("Successful") >= 0)
{
alert("Post Made");
window.location = "index.php"
}
else
{
alert(reply_text);
}
}
});
}
</script>
<div id="content">
<?php
if (loggedin())
{
$ID = getID();
$query = "SELECT * FROM `posts`";开发者_JS百科
$result=mysql_query($query);
$count=mysql_num_rows($result);
$users = "SELECT `userID` FROM `users`";
$resultID=mysql_query($users);
while ($row = mysql_fetch_array($result))
{
echo '<div class="posts">';
echo $row['2']."<br /><br />";
echo '<div class="posts_bottom">';
echo '<p class="name">';
echo showuser($row['1'])."</p>";
echo '<p class="rate">';
echo '<input type="submit" value="+1"/></p>';
echo '<p class="points">';
echo showpoints($row['1'])."</p>";
echo "</div>";
echo '</div>';
}
echo
'<div id="makepost">
<br /><textarea rows="3" cols="25" id="post_text" ></textarea><br />
<input type="submit" id="post_bttn" value="Post" onclick="postdata(); return false;">
</div>';
As they are put into a new div everytime I don't know what to refresh? Such as if it was one div I could jus refresh that, but these are being created and I don't know how many would need to be loaded.
Any adivce? Thanks alot :D
Simple, you have to set timmer in java script to execiute function every 10 minutes.
setTimeout("updateMyFBLikeChat()",60000);
Don't forget that is not the same function that you execute when posting data. More you can read in w3schools.com
You need to carry out below steps to get basic understanding and rest you can improvize..
- Create a PHP file (may be name as update.php).
Perform login session check and query database for posts.
$query = "SELECT * FROM
posts
"; $result=mysql_query($query); while ($row = mysql_fetch_array($result)){ .... }- Return the complete HTML with echo statement.
Call this PHP file with AJAX under settimeout function with the delay you want.
function updatePageContent(){ $.ajax({ type: "POST", dataType: "text", url: "update.php", ... .... } $(body).on('load', function(){ setTimeout("updatePageContent()",60000); });
On success function of this ajax call you can update your DOM.
$(your-div).html(responseData);
However, this is just a basic example to let you start. Once you are comfortable with this approach, you can modify your script to send the last post ID to update.php and only return the posts which are newer than that.
精彩评论