1

I have a simple PHP script that allows users to comment on a page. This works fine, but I am having difficulties adding a "like button" to it.

The contents for each comment are stored in a database. Here's the code I used for it just to give you an idea

CREATE TABLE IF NOT EXISTS `comments` (
`id` int(128) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=101675,
  `postername` varchar(16) NOT NULL,
  `postmessage` varchar(400) NOT NULL,
  `posterip` varchar(128) NOT NULL,
  `postdate` varchar(128) NOT NULL,
  `likes` int(100) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=101675 DEFAULT CHARSET=latin1;

And here is something what my the code for my like button would look like:

<a href='#' class='likecomment' onclick='likecomment();' id='".$id."'>Like</a>

the PHP variable $id matches the value of the ID column in my database. So, for example, let's say we had a row with an ID of 1, then the ID for the like button for the HTML of that comment would also be 1.

My thinking was that if they matched, when people clicked on it I could use javascript to get the ID and then include a PHP file that connects to the database and selects the row that contains that ID in the ID column. I don't need help with connecting to the database, but would appreciate help with the javascript that gets the ID of the like button clicked and then passes it to my PHP script so I can select the proper row to give the like to.

In short, I need it so when they click the like button, it increases the value of the likes column by 1. I know the PHP to do this, but need help with the jquery / javascript. Much appreciated. Here is what I have so far with, although it isn't much.

function likecomment() {
    $.get("like.php");
    return false;
}
1
  • "Get" should be used for retrieving data while instead you should do a "POST" to send the update to the php to state that a "User" wants to "like" this comment. The POST body can be empty and your PHP can query the URI to retrieve the ID but either way best practices would frown on GET used for anything outside retrieval. Commented Oct 24, 2015 at 20:35

2 Answers 2

1

You could have html:

<a href='#' class='likecomment' onclick='likecomment(".$id.");' >Like</a>

And then JS:

function likecomment(id) {
    $.get("like.php?id="+id);
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can do like that way also. Just you need to add some more code in it: Add "this" in "likecomment()" function.

<a href='#' class='likecomment' onclick='likecomment(this);' id='".$id."'>Like</a>

Get the id like that way :

function likecomment(obj) {
    var id = $(obj).attr('id');
    $.get("like.php");
    return false;
}

You can send data :

$.get( "like.php", { like_button_id: id } );

now you will access id in you like.php file like that :

$id = $_GET['like_button_id'];

Echo id on page :

Suppose you have a span in you DOM.

<span class="like_button_id"></span>

$.get( "like.php", { like_button_id: id } )
  .done(function( data ) { 
    alert( data );
    $(".like_button_id").html(data);
  });

data object will contain your id. Now after that you will through this id on our page html like that :

Now put your like button id on this span using this code in jquery :

$(".like_button_id").html(data);

5 Comments

So on like.php how would I save the javascript variable ID to a php variable, for example $id = [id here] ?
You can send data like : $.get( "like.php", { id: id } );
@JasonHunn: your most welcome. If you thing the solution is right then please mark it as correct.
Actually for that you need to make changes in our $.get() function.
@JasonHunn: You will get complete reference regarding $.get(); function at this link : api.jquery.com/jquery.get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.