Jason Hunn
Jason Hunn

Reputation: 41

PHP / javascript like button

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;
}

Upvotes: 1

Views: 1477

Answers (2)

Lakhan
Lakhan

Reputation: 13226

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);

Upvotes: 0

Piotr Borek
Piotr Borek

Reputation: 864

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;
}

Upvotes: 1

Related Questions