简体   繁体   English

PHP处理包含随机项目的清单

[英]PHP handle a checklist with a random amount of items

I have a HTML Table which is used as a checklist. 我有一个HTML表格用作检查清单。 There are multiple checklists and user can create their own checklist and items as well. 有多个清单,用户也可以创建自己的清单和项目。 This checklist have a random amount of rows. 此清单有随机的行数。

$table .= "<tr style='cursor: pointer; border: 1px solid black;' onclick='selectVal(this)'>
                          <td style='border: 1px solid transparent; width: 5%;' class='cbTrue'><input type='hidden' value='".$srcarr['id']."' name='item_id' /><input type='checkbox' name='".$srcarr['id']."_True' /></td>
                          <td style='border: 1px solid transparent; width: 30%;'>".$srcarr['item']."</td>
                          <td style='border: 1px solid transparent; width: 30%;'><input type='hidden' class='ckTimeStamp' /></td>
                     </tr>";

$srcarr is my array which comes from a MySQLI query and contains the information of the ID of an Item or the Name oft an Item. $srcarr是我的数组,它来自MySQLI查询,并包含项目ID或项目名称的信息。

My problem is: I want to save the items with the information if they have been checked and at which time in a MySQL Database. 我的问题是:如果要检查项目以及何时将其与信息一起保存在MySQL数据库中,我想将其与信息一起保存。 But I have no idea how to know after the form have been posted to PHP how I can separate the information about the single items. 但是我不知道如何将表单发布到PHP之后如何区分有关单个项目的信息。 Does anybody have an idea? 有人有主意吗?

Did you mean the exact time when user click the checkbox not the moment when they submit the pages? 您是指用户单击复选框的确切时间,而不是用户提交页面的时间吗?

If it is the exact moment they check your checkbox you could use javascript to assign the time value on hidden input by adding it on event change at checkbox element. 如果正是他们选中您的复选框的那一刻,您可以使用JavaScript通过在复选框元素的事件更改上添加时间来为隐藏的输入分配时间值。

on jquery it will be like: 在jQuery上,它将像:

$('input[type="checkbox"][name="checkbox-name"]').change(function() {
    if(this.checked) {
        var now = new Date;
        $('input. ckTimeStamp').val(now.getTime);
    }
});

As I understood, you have some rows to add and you need to create a unique name for each input. 据我了解,您需要添加一些行,并且需要为每个输入创建唯一的名称。

Try this: 尝试这个:

<input type='hidden' value='".$srcarr['id']."' name='item_id[".$srcarr['id']."]' /><input type='checkbox' name='".$srcarr['id']."_True' />

Then in the form processing, loop through $_POST['item_id'] and from keys of the array check if is set $_POST[$key . '_TRUE'] 然后在表单处理中,循环$_POST['item_id']并从数组的键中检查是否设置了$_POST[$key . '_TRUE'] $_POST[$key . '_TRUE']

See Processing HTML checkbox forms with PHP for code example. 有关代码示例,请参见使用PHP处理HTML复选框表单

What you need is to have a list of checkbox with the same name like custom_checkboxes[] and store $srcarr['id'] as a value. 您需要的是具有与custom_checkboxes[]相同名称的复选框列表,并将$srcarr['id']作为值存储。 In PHP, when you receive the data as an array, only those $srcarr['id'] that was checked will be included inside the array. 在PHP中,当您以数组形式接收数据时,只有被检查的$srcarr['id']将被包含在数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM