0

I have some post and like button with code:

<a href="likes.php?userid='$userid'&postid='$postid'">Like</a>

Now In the likes.php I have some Get coding for userid and postid to find which user liked which post and to store it into database. It works good but it reload page. It goes to likes.php and if it is successful it header back to home page or any other I want to. Now my question is how can I do it without reloading a page should I include likes.php code into page where are the posts and like buttons. And use a <a ref=""></a> if it is possible that way? Or if somebody have a better explanation he can post it as well.

Noob
  • 1
  • 5

1 Answers1

0

Yes because you used the code ahref tag and this is how it is supposed to work.If you dont want it to reload try using AJAX.This is not the complete answer but you need to make that work through ajax only.You will find a lot of videos and demos regarding it.

Check this Example: http://www.w3schools.com/ajax/ajax_php.asp

Update:

Mainfile.php

<html>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">   </script>
<?php
session_start();
$userid=$_SESSION['userid'];
?>

<div id="like">

<a href="#" id="postid"  onclick="likeclick(this);">Like</a> 


    <!-- here id should be different for every post .I would prefer using    post id to this ahref id because i would use that to detect what post it is actually.  -->
 </div>
 </html>


 <script type="text/javascript">
function likeclick(element)
{
    var postid=element.id;
    var userid=<?php echo json_encode($userid);?>;
    //You can use different methods to pass variable to javascript.I used this one because it is easy to implement
    //it has some cons to it .Do check for that on google also.
    //http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript
    //Check this link for more.

    $.ajax({
        type:'POST',
        url:'getlike.php',
        data:{"userid":userid,"postid":postid},
        success : function(content)
        {
                $('#like').html(content);
            //This gets the html content from the getlike page and displays in the div on this page.
            //Note:I have used .html which replaces any previous content inside the 'like' div.
    })

}
</script>

getlike.php

<?php
require 'connect.inc.php'; //This make a connection to the database
$userid=$_POST['userid'];
$postid=$_POST['postid'];

$statement=$mysqli->prepare("select `likes` from `posts` where `postid`=?");
$statement->bind_param("s",$postid);
$statement->execute();
$result=$statement->get_result();
while($row=$result->fetch_assoc())
{
   $likes_on_this_post=$row['likes'];
}
$likes_on_this_post=$likes_on_this_post+1; //Added one like more.
$statement=$mysqli->prepare("update `posts` set `likes`=?");
$statement->bind_param("s",$likes_on_this_post);
$statement->execute();
echo "+".$likes_on_this_post;
 //This echo is actually the main thing.When this page runs through ajax the    response is given back to the calling object
//What goes with response are the html contents on this page as well as whatever i echo on this page.
//Considering this example i have only echoed the no of likes and it doesn't contain any sort of html content so only the echoed element goes.
?>
Tilak Raj
  • 1,369
  • 5
  • 31
  • 64
  • Can I use ajax get var with ref? THis may be stupid question but when I use iframe with Page It load page inside iframe. Can ref works out of iframe like anywhere on page and then with ajax get var to get userid from ref? – Noob Feb 29 '16 at 17:32
  • to be true I didn't even get what you just said but you can do everything with PHP and ajax.I mean the real answer to your question is using PHP and ajax.Trust me, Go watch some videos and read some docs .It will surely help you in the long run. :) – Tilak Raj Feb 29 '16 at 17:38
  • I found something from phpacademy its not exacly what I need but it would help lot. Thanks anyway – Noob Feb 29 '16 at 17:45
  • See i can give you the whole working code for this but then you won't get a thing. – Tilak Raj Feb 29 '16 at 17:47
  • Give me ofc I wont use it but its easier to try to understand how is working and then make my own then pasting just in code .. If you can .. – Noob Feb 29 '16 at 17:56
  • Would you mind if i give you after 12 hours? I will update my answer then.. Actually i have a paper tomorrow and i have to concentrate on that. So after 12 hours fine? – Tilak Raj Feb 29 '16 at 17:58
  • Yeah I have lot other things to do that is stopping me .. Thanks btw – Noob Feb 29 '16 at 18:02
  • @Noob Done.Check my answer. – Tilak Raj Mar 01 '16 at 09:17
  • Thanks man it helped me but I needed this part of code thanks :D $.ajax({ type:'POST', url:'getlike.php', data:{"userid":userid,"postid":postid}, success : function(content) { $('#like').html(content); }) – Noob Mar 01 '16 at 17:22
  • No problem. You can upvote it and select it right if you find it right :) – Tilak Raj Mar 01 '16 at 17:23