[英]How can I loop over form data using serialized (jQuery / Ajax)
我正在嘗試獲取帖子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);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.