i having a little issue firstly, please ignore my depreciated functions i am trying to run PHP function continuously i have a button when clicked triggers an Ajax script which runs properly the Ajax page inserts the like into the db however i need to show the user that his like has counted but the like function count script is not updating until i reload the page, is there a way for me to continuously reload the PHP function so if any new like enters the db it updates, thanks in advance.
//php function for counting likes functions.php
function likes_count($postid){
set_time_limit(0);
$g = mysql_query("SELECT COUNT(*) FROM postlikes WHERE postid = $postid") or die (mysql_error());
$co = mysql_fetch_array($g);
$count = $co[0];
echo $count;
}
//like button index.php where the image post appears for the user to like
<button style="margin-left:13px;" id="<?php echo $postid;?>" class="col-md-5 n btn btn-default btn-xs like"><i id="f" class="fa fa-thumbs-o-up"></i> <span id="like_<?php echo $postid ?>"><?php echo likes_count($postid); ?> </span> Likes</button>
//
//ajax for calling the like page on index.php
<script>
$('.like').on('click', function (e){
var userid = "<?php echo $ida ?>";
var postid = $(this).attr('id');
if ((postid == "")) {
alert("no info bro");
} else {
$.ajax({
type: "POST",
url: "like.php",
data: {postid: postid, userid: userid},
cache: false,
});
}
e.preventDefault();
});
</script>
//like page it self like.php
<?php
include "connect.php";
mysql_select_db("unilag");
if(isset($_POST['postid'])) {
$postid=$_POST['postid'];
$id=$_POST['userid'];
$e = mysql_query("SELECT * FROM postlikes WHERE userid = $id AND postid = $postid") or die(mysql_error());
if(mysql_num_rows($e) > 0) {
// remove like&
$re = mysql_query("DELETE FROM postlikes WHERE userid = $id AND postid = $postid") or die (mysql_error());
$notify = mysql_query("DELETE FROM notification WHERE nfrom = $id AND type = 'like_post' AND postid = $postid") or die (mysql_error());
} else {
// like post
$re = mysql_query("INSERT INTO postlikes SET userid = $id, postid = $postid, date = now()") or die (mysql_error());
$rr = mysql_query("SELECT userid FROM post WHERE post_id = $postid");
$y = mysql_fetch_array($rr);
$iii = $y['userid'];
$notify = mysql_query("INSERT INTO notification SET nfrom = $id, nfor = $iii, type = 'like_post', postid = $postid, seen = 'no', date = now()") or die (mysql_error());
}
echo likes_count($postid);
}
?>