Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like Button: Pass Certain Variable from PHP to Jquery when Clicked

I have this simple php code that displays all users' posts with all of them having a unique 'like' button:

$sql = "SELECT * FROM posts";
$result = mysqli_query($con,$sql);

while($row=mysqli_fetch_assoc($result)){
  $post_content = $row['content'];
  $post_id = $row['id'];

  echo $post_content ."<br/>";
  echo "<a href = '#' id = 'likebutton' onclick = 'like_add($post_id )>Like</a>";
}

And my javascript:

function like_add(post_id){
  alert(post_id); //to test if the link is working
}

I got this error however:

ReferenceError: 2dg7c is not defined

Here 2dg7c is an id of a post in which I clicked LIKE.
Did I missed something?

like image 826
Dranreb Avatar asked May 17 '26 17:05

Dranreb


1 Answers

Change link code like this:-

echo "<a href='#' id='likebutton' onclick='like_add(\"{$post_id}\")'>Like</a>";
like image 158
Anant Kumar Singh Avatar answered May 19 '26 07:05

Anant Kumar Singh