我正在尝试获取帖子 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最佳答案
由于您在序列化时使用#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);
}
});
});
关于javascript - 如何使用序列化(jQuery/Ajax)循环表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37381476/