记住复选框值,如果它在表单php中检查了数组名称


remember checkbox value if its checked array name in a form php

假设我们有这个:

echo '<form method="post">
        <div class="form-group">
            <table class="table table-bordered table-hover table-striped" style="width:auto">
              <tr>
                <td><label for="array">ARRAY_NAME</label></td>        
                <td>
                 <input type="checkbox" name="array[]" value="1" /> option1<br />
                 <input type="checkbox" name="array[]" value="2" /> option2
                </td>
              </tr>
              <tr>
                <td><label for="array2">ARRAY_NAME2</label></td>        
                <td>
                 <input type="checkbox" name="array2[]" value="1" /> option1<br />
                 <input type="checkbox" name="array2[]" value="2" /> option2
                </td>
              </tr>
              <tr>
                <td><label for="array3">ARRAY_NAME3</label></td>        
                <td>
                 <input type="checkbox" name="array3[]" value="1" /> option1<br />
                 <input type="checkbox" name="array3[]" value="2" /> option2
                </td>
              </tr>
           </table>
        </div>
        <button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';

我尝试实现这个代码:<?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>

但没有用!只有当您有name="array1"name="array2"时,它才有效。通过这种方式,我想我可以在保存的父数组中保存多个数据!就像这个form[array1[],array2[],array3[]]

有人能给我一个解决方案吗,因为我被卡住了!提前感谢各位!!

您试图错误地访问这些值。

你可以访问这样发布的数组:

echo $_POST['array1'][0];

php.net/$_POST查看第一条注释。

当您将方括号放在表单控件的名称中时,PHP会将具有相似名称的元素集扩展为数组。

您需要访问:

$_POST['array1'][0]

不是

$_POST['array1[0]'] // This is incorrect

(您还需要匹配控件的实际名称:您的HTML有arrayarray2array3,但没有array1