表单上的复选框是基于db查询选中的,如果验证失败则不会重置


Checkboxes on form checked based on db query, not to be reset if validation fails

我有一个表单供用户更新他们希望在其中进行服务的区域(最多4个)。该表单允许用户更新数据库中的服务区域。使用strpbrk(),我可以在页面加载时成功地检查当前区域,但如果表单验证失败,则重新检查原始复选框。该表单包含132个区域。

查询返回:$row['location'] = "cityone.city two."

形式:

<input type="checkbox" value="Accomack" name="T_ordernum[Accomack]" <?php if(isset($_POST['T_ordernum']) && is_array($_POST['T_ordernum']) && in_array('Accomack', $_POST['T_ordernum'])) echo 'checked="checked"'; ?> /> 
<label>Accomack</label><br/>
//repeated for all 132 areas

我已经在其他表单上这样做了…用户可以看到并更新他们的信息,如果验证失败,他们的编辑将被回显,但不是从数组中。有人有什么建议吗?

这有点长,但我明白了。我将查询结果放入具有explode()的数组中,并检查区域是否位于具有in_array()的数组中:

$z = explode('.',$row['territories']);
if (in_array("Accomack",$z)){$z1 = "checked";}
if (in_array("Albemarle",$z)){$z2 = "checked";}
if (in_array("Alexandria",$z)){$z3 = "checked";}
// continue for remaining areas

我将PHP从表单输入移动到POST验证:

if(isset($_POST['T_ordernum']) && is_array($_POST['T_ordernum']) && in_array('Accomack', $_POST['T_ordernum'])){$z1 = "checked";}else{$z1 = '';}$_POST['T_ordernum'])){$z1 = "checked";}else{$z1 = '';}
if(isset($_POST['T_ordernum']) && is_array($_POST['T_ordernum']) && in_array('Albemarle', $_POST['T_ordernum'])){$z2 = "checked";}else{$z2 = '';}
if(isset($_POST['T_ordernum']) && is_array($_POST['T_ordernum']) && in_array('Alexandria', $_POST['T_ordernum'])){$z3 = "checked";}else{$z3 = '';}
//continue for remaining areas

并在适当的输入中返回"checked"或":

<input type="checkbox" value="Accomack" class="other" name="T_ordernum[Accomack]" <?php echo $z1;?> />
  <label class="left">Accomack</label><br/> 
<input type="checkbox" value="Albemarle" class="other" name="T_ordernum[Albemarle]" <?php echo $z2;?> />
  <label class="left">Albemarle</label><br/> 
<input type="checkbox" value="Alexandria" class="other" name="T_ordernum[Alexandria]" <?php echo $z3;?> />
  <label class="left">Alexandria</label><br/>
//continue for remaining areas
D

: