保持PHP表单中的复选框 [英] Keeping checkboxes checked in PHP form

查看:182
本文介绍了保持PHP表单中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在表单提交时检查复选框是否有错误,但现在有不同的问题。我使用相同名称的复选框,人们可以点击1或多个。我希望它只保留用户检查填充的复选框,如果有一个问题,如果他们检查q1 A q1B但不是q1c我只想要q1 A和Q1b显示检查时重新加载错误。现在出现错误,所有相同名称的复选框都被选中。我已经尝试更改名称为q1 [],但是没有工作。

I know how to keep the checkboxes checked on form submit when there is an error, but I have a different issue now. I am using check boxes of the same name where people can click 1 or multiple. I want it to only keep the check boxes that the user checked filled if there is a problem like if they check q1 A q1B but not q1c I want only q1 A and Q1b to show checked when reloaded on an error. Right now on an error all checkboxes of the same name are checked. I have tried changing the name to q1[] but that did not work. Can you please take a look and let me know how to fix this?

这是我的代码。

    <tr>
<td style="width: 124px" class="style15">Tape Recorder<?php    if(isset($problems['tape[]'])) {?><font color="red">*</font><?php } ?></td>
<td class="style9"> 
    <input name="tape[]" id="tape1" type="checkbox" value="used before," <?php   if(isset($_POST['tape[]'])) echo "checked"; ?> 
  </td>
<td class="style9"> 
    <input name="tape[]" id="tape2" type="checkbox" value="helpful in past,"        <?php   if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9"> 
    <input name="tape[]" id="tape3" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9"> 
    <input name="tape[]"  id="tape4" type="checkbox" value="NA" <?php if(isset($_POST['tape[]'])) echo "checked"; ?></td>
 </tr>
  <tr>
        <td style="width: 124px">Note Taker <?php if(isset($problems['note'])) {?>      <font color="red">*</font><?php } ?></td>
<td class="style9"> 
        <input name="note" type="checkbox" value="used before," <?php if(isset($_POST['note'])) echo "checked"; ?> 
</td>
<td class="style9"> 
    <input name="note" type="checkbox" value= "been helpful in the past," <?php if(isset($_POST['note'])) echo "checked"; ?> 
<td class="style9"> 
    <input name="note" type="checkbox" value= "requesting from DACC" <?php if(isset($_POST['note'])) echo "checked"; ?> 
<td class="style9"> 
    <input name="note" type="checkbox" value="NA" <?php if(isset($_POST['note'])) echo "checked"; ?> 
</tr>


推荐答案

一个快速而肮脏的解决方案是:

A quick and dirty solution would be:

<input name="tape[]"  id="tape[]" type="checkbox" value="NA" <?php if(isset($_POST['tape']) && is_array($_POST['tape']) && in_array('NA', $_POST['tape'])) echo 'checked="checked"'; ?> />

因为你需要为每个不同的答案改变'NA'部分。虽然我会看看像一个循环的重复的复选框或回调函数,以确定是否回显 checked = checked

For that you need to change the 'NA' part for each different answer obviously. Though I would look at something like having a loop for repeated checkboxes or a callback function to determine whether or not to echo checked=checked.

这篇关于保持PHP表单中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆