繁体   English   中英

记住复选框值,如果其检查的数组名称采用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'" : "" ?> <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>

但这没用! 仅当您具有name="array1"name="array2"它才有效。 这样我想我可以将多个数据保存在保存的父数组中! 像这样的form[array1[],array2[],array3[]]

有人可以给我一个解决方案,因为我被卡住了! 在此先感谢大家!

当您在窗体控件的名称中放置方括号时,PHP会将具有相似名称的那组元素膨胀到数组中。

您需要访问:

$_POST['array1'][0]

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

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

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

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

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

php.net/$_POST参见第一条评论。

暂无
暂无

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

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