I'm trying to create this like button which goes +1 after clicking on it. You can only click the like once (like is bonded to the account with what u logged in, so a user can not 15x like the same post.)
In my HTML I have this button
<a class="like" id="<?php echo $rows['id']; ?>" href="index.php?id=.$rows['ID']">like</a>
As my AJAX/JQuery I have these
$('a.like').on('click',function () {
var id = $(this).attr('id');
$.ajax({
url:"ajax/post_like.php",
method: "POST",
data: ({ id: id }), // first id is the name, second is the actual id variable we just created
beforeSend: function(data) {
// you can do stuff in here before you send the data, display spinner gif etc
alert('sending the like');
},
success: function(data) {
// same here but when the ajax request is successful
// the data variable is coming from the echo of your PHP script
alert(data);
},
complete: function(data) {
// yet again but on completion
alert('completed the like');
}
});
// stops the browser following the link (href) in the a tag
return false;
});
Now here is the part where I am struggling, mainly the PHP handling. We have created a load more button which loads more posts which works well. The code is as follows. How can I know work out the like part in the same way as the load more button?
<?php
include_once("../classes/db.class.php");
session_start();
$userid = $_SESSION['userid'];
$output = "";
$limit = $_POST['limit'];
if(isset($limit)){
if($limit != ""){
$conn = db::getInstance();
$query ="SELECT posts.id, posts.post_title, posts.picture ,posts.description, posts.location, posts.post_date
FROM posts
INNER JOIN friends
ON posts.user_id = friends.user1_id OR posts.user_id = friends.user2_id
WHERE friends.user1_id='$userid' OR friends.user2_id='$userid'
ORDER BY posts.post_date DESC
LIMIT $limit";
$result = $conn->prepare($query);
$result->execute();
while($row = $result->fetch()) {
$output.=' <div class="post">';
$output .='<div class="post_desc"><p>'. $row['post_title'].'</p></div>';
$output .='<div class="post__picture"><img src="'. $row['picture'].'" alt=""></div>';
$output .='<div class="post_desc"><p>'. $row['description'].'</p></div>';
$output .=' <div class="post_date">'. $row['post_date'].'</div>';
$output .='</div>';
};
echo $output;
}
}
else{
}
?>