我創建了一個表單,允許用戶選擇他們想要接收的通知,然后將此信息存儲到數據庫中。我意識到目前用戶可以接收大約 10 個選項/通知,因此為每個用戶將 10 個選項保存到數據庫中,將很快填滿數據庫。相反,我想讓它只將他們不想接收的通知類型存儲到數據庫中。目前,jQuery 只發送選中復選框的值,不發送未選中復選框的值。有沒有其他人有更好的解決方案,或者對我如何發送未檢查的值有任何想法,或者可能將其創建為 unchecked = "off" 和 checked = "on"查詢var data = $('#notificationsform').serializeArray();$.ajax({ type: "POST", url: base_url + "/api/save_notification_preferences", data: data, dataType: 'json', cache: false, success: function (response) { var data = response.data; if (response.status == "success") { console.log(data.message); $('#successtxt').html(data.message); $('#success-snack').addClass('snackbar-active'); setTimeout(function () { $('#success-snack').removeClass('snackbar-active'); }, 1500) $(".close-article").click(); } else { $('#errortxt').html(data.message); $('#error-snack').addClass('snackbar-active'); } }});PHP$notifications = $this->input->post('notifications');$me = is_user_logged_in();$delete1 = "DELETE FROM `notificationssettings` WHERE userID ='$me' ";$this->db->query($delete1);foreach ($this->input->post('notifications') as $key => $bg): $insertnotifications['type'] = $bg; $insertnotifications['userID'] = is_user_logged_in(); $this->db->insert("notificationssettings", $insertnotifications);endforeach;$data = [ 'status' => 'success', 'data' => [ 'message' => "Notification settings saved succesfully.", ],];echo json_encode($data);exit();
1 回答

慕萊塢森
TA貢獻1810條經驗 獲得超4個贊
未選中的復選框值根本不會發送到服務器。
然而,由于您知道可能的選項是什么,您可以讓您的 PHP 后端知道可能的選項,然后可以將已知選項列表與通過 POST 請求傳入的選項進行比較。
<?php
// ...
$POSSIBLE_NOTIFICATIONS = [
'reviews',
'feed',
'follows',
'updates',
'invites',
'events',
'inviterequests',
'invitestatus',
'othersocial'
];
$notifications_opted_in = $this->input->post('notifications');
$notifications_opted_out = array_diff($POSSIBLE_NOTIFICATIONS, $notifications_opted_in);
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
0/150
提交
取消