亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 PHP 中獲取未選中的復選框值

在 PHP 中獲取未選中的復選框值

PHP
守著星空守著你 2023-05-12 14:27:41
我創建了一個表單,允許用戶選擇他們想要接收的通知,然后將此信息存儲到數據庫中。我意識到目前用戶可以接收大約 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);



查看完整回答
反對 回復 2023-05-12
  • 1 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號