on each post page has a button to "like" how can I check if the post was liked and change the button name, for example, "liked" and if not, by clicking the button would be done, but everything in the background, I have very shallow knowledge of javascript, but I know I'll need ajax for it. already set up a small script in php / mysql that returns a json { "favorite": 1}
when the post is already liked or changes to liked, and its natural state is { "favorite": 0}
Because you can only like the post being logged into the site, the user id will be set by session, and the id of the post will be sent using the POST method
Ex:
<a href="site.com/post/like"> Favorite </a>
... Sorry for my bad english
set the status on the server I am assuming the status is in the $favorite var <a id="1234" href="site.com/post/like"
class="like <?PHP echo $favorite?"liked":""; ?>">Favorite</a>
call when clicked
like this
$(function() {
$(".like").on("click",function() {
$.post("likeornot.php?id="+this.id,function(data) {
$(this).toggleClass("liked",data.favorite==1);
// you can set the html of the link here too
});
});