我有这个简单的 php 代码,它显示所有用户的帖子,并且所有用户的帖子都有一个独特的 'like' 按钮:
$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>";
}
还有我的 JavaScript:
function like_add(post_id){
alert(post_id); //to test if the link is working
}
但是我收到了这个错误:
ReferenceError: 2dg7c is not defined
这里2dg7c
是我点击LIKE
的帖子的ID。
我错过了什么吗?
最佳答案
像这样更改链接代码:-
echo "<a href='#' id='likebutton' onclick='like_add(\"{$post_id}\")'>Like</a>";
关于javascript - 点赞按钮: Pass Certain Variable from PHP to Jquery when Clicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45034104/