将未选中的复选框发送到 PHP
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您首先创建了复选框,所以您知道复选框的总数。
总复选框减去选中的复选框将为您提供您想要的内容,即未选中的框。
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.
使用 3 个数组,其中 1 个保存您要发送的选项,一个保存用户的响应,另一个空数组保存差异。两者之间的差异将为您提供未经检查的字段。
注意:您可以将 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.
Note: you can replace the foreach with array_diff as @deceze mentioned in the comment.