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?
echo "<a href = '#' id = 'likebutton' onclick = 'like_add($post_id )>Like</a>";in this line$post_idis a string and you are passing without quotes so it will not work. you have to use `\`alert()for troubleshooting., useconsole.log()instead.