将未选中的复选框发送到 PHP

发布于 2024-12-09 07:18:20 字数 386 浏览 0 评论 0原文

我的 HTML 表中有一个复选框字段。表是动态生成的,字段存储在数组中,如下所示:

在 PHP 中,我正在将已检查的行插入表中,但我还需要在提交后向用户显示尚未检查的行,但无法知道自 checked[]< 以来哪些行未通过/code> 表示未选中的复选框不会被提交。

我想要实现的是向用户显示一个包含多行的表,他检查要添加到数据库中的行。表单提交后,页面将显示哪些行被插入以及哪些行未被用户选择。未选中的行不需要插入到任何数据库中,但应在提交后立即向用户显示一次,以便他可以打印页面以进行记录。

解决这个问题的最佳方法是什么?

I have a checkbox field in my HTML table. The table is generated dynamically, and the field is stored in an array as follows:

<input type="checkbox" name="checked[]" value="1">

In the PHP, I am inserting the checked rows into the table, but I also need to display the rows that have not been checked to the user after the submission, but there is no way of knowing which rows were not passed since checked[] for unchecked checkboxes are not being submitted.

What I want to achieve is the user is displayed a table with multiple rows, he checks which rows he wants to add to the database. After form submission, a page is to display which rows were inserted and which rows were not selected by the user. The unchecked rows need not be inserted into any database, but should be displayed to the user only ONCE, right after the submission, so that he can print the page for record purpose.

What is the best way to tackle this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-12-16 07:18:20

因为您首先创建了复选框,所以您知道复选框的总数。

总复选框减去选中的复选框将为您提供您想要的内容,即未选中的框。

unchecked boxes = total checkboxes -  checked checkboxes ;

since you are creating the chekbox in first place so you know the total number of checkbox.

Total Checkbox minus Checked checkbox will give you what you want i.e unchecked box.

unchecked boxes = total checkboxes -  checked checkboxes ;
夜空下最亮的亮点 2024-12-16 07:18:20

使用 3 个数组,其中 1 个保存您要发送的选项,一个保存用户的响应,另一个空数组保存差异。两者之间的差异将为您提供未经检查的字段。

<?php

$myOptions = array('option1', 'option2', 'option3');
$userArray = array('option2'); // This is your $_POST['checkboxes'] array
$leftOptions = array();

foreach ($myOptions as $value){
  if (!in_array($value, $userArray)){
    $leftOptions[] = $value;
  }
}

?>

注意:您可以将 foreach 替换为 array_diff,如评论中提到的@deceze。

Use 3 arrays, 1 holding the options you are sending, one with the responses from the user and one empty that will hold the difference. The difference between the two will get you the unchecked fields.

<?php

$myOptions = array('option1', 'option2', 'option3');
$userArray = array('option2'); // This is your $_POST['checkboxes'] array
$leftOptions = array();

foreach ($myOptions as $value){
  if (!in_array($value, $userArray)){
    $leftOptions[] = $value;
  }
}

?>

Note: you can replace the foreach with array_diff as @deceze mentioned in the comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文