我尝试获取帖子ID并在用户点击添加到收藏夹按钮后将其插入数据库。
所有帖子都返回id 30的问题,这是数据库中的最后一个id。
我的php / html代码
while($row = mysqli_fetch_array($get_posts)) {
<i style="float:right;" class="fa fa-pinterest-square">
<form id="pin_post" method="post"> ';
if (isset($_SESSION['user_id'])){
<input type="hidden" value="'. $_SESSION['user_id'].'" name="user_id" />
}
<input type="hidden" value="'.$row['post_id'].'" name="post_id[]" />
</form>
</i>
jQuery / Ajax代码
<script>
$(document).on("click",".fa-pinterest-square",function(e) {
$.ajax({
url: "includes/test.php",
type: "POST",
data: $('#pin_post').serialize(),
success: function(data) {
alert(data);
}
});
});
</script>
点击任意帖子后,我在数据库中获得了最后一个ID。
点击
,请帮助我获取每个帖子的ID答案 0 :(得分:1)
由于您在序列化时使用#pin_post
,因此当jQuery查找#pin_post
时,您将获得最后一次命中。您应该使用唯一的ID或类似的东西
$(document).on("click",".fa-pinterest-square",function(e) {
var form = $(this).find('form'); //Since the form is child to <i>
$.ajax({
url: "includes/test.php",
type: "POST",
data: form.serialize(),
success: function(data) {
alert(data);
}
});
});