将一个值数组作为发布数据传递给document.forms[].method

Pass an array of values as post data to document.forms[].method?

本文关键字:数据 document method forms 一个 数组 布数据      更新时间:2023-09-26

在我的html中,我有这个,

    <tr>
        <td class="grid_cell" width="5%">
            <input type="checkbox" name="workspace_trees_rpt_target" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
        </td>
    </tr>

这是在一个循环中,因此将显示许多具有不同值的复选框。在我的脚本中,我有这个,

    if(confirm("Delete cannot be undone, click OK button to proceed.")) {
        document.forms[0].method="POST";
        document.forms[0].action="delete.php";
        document.forms[0].submit();     
    }

选中其中两个复选框并单击"确定"按钮后,我将转到delete.phpprint_r我的帖子数据。但当显示print_r的结果时,它只显示了选中复选框的一个值,但我预计会有两个。如何使我选中多个复选框时,复选框的发布数据将是选中复选框的值数组?

您必须将输入的名称设置为数组:

name="workspace_trees_rpt_target[]"

您为所有复选框指定了相同的名称。因此只能返回一个值。

更改为

<input type="checkbox" name="workspace_trees_rpt_target[]" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />

然后在您的PHP代码中,您将获得$_POST数组['workspace_trees_rpt_target'][]

这样处理:

if ( isset( $_POST['workspace_trees_rpt_target'] ) ) {
    // at least one checkbox has been ticked
    foreach ( $_POST['workspace_trees_rpt_target']  as $checkboxe ) {
         // do whatever $checkbox it will be set to the value="" that you set earlier
    }
}