保持ASSOC阵列复选框检查 [英] keep assoc array checkbox checked

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

问题描述

我想保持复选框选中的列表,直到后是有效的,没有错误。下面是code我使用。我会很感激的任何帮助。

I'm trying to keep a list of checkboxes checked until post is valid and no errors. Below is the code I'm using. I would be thankful for any help.

     <?php foreach ($drinks_checkbox as $option => $options){  ?>

     <input type='checkbox' id='drinks[]' name='drinks[]' value='<?php echo $option;?>'   <?php if(!empty($_POST['drinks'])){if($_POST['drinks']==$option){ echo "checked='checked'" ; }}?> /><?php echo $options;?><br />
    <?php } ?>

我可以使用但破灭,我需要帮助上述成功显示选中的复选框。

I can successfully display the checked checkboxes using implode however I need help for the above..

推荐答案

$ _ POST ['饮料']是一个数组。另外,id值不需要是饮料[]'

$_POST['drinks'] is an array. Also, the id value does not need to be 'drinks[]'

尝试是这样的:

$drinksIndex = 0;
$drinksPost = $_POST['drinks'];
foreach ($drinks_checkbox as $option => $options){  ?>

  <input type='checkbox' id='drinks<?php echo $drinksIndex; ?>' name='drinks[<?php echo $drinksIndex; ?>]' value='<?php echo $option;?>'<?php
  if( !empty($drinksPost[$drinksIndex]) ) echo " checked='checked'";
  $drinksIndex++;
?> /><?php
echo $options;?><br />
<?php
} ?>

修订的!空($ drinksPost [$ drinksIndex])的一部分,改为联想。

Amended the !empty($drinksPost[$drinksIndex]) part and changed to associative.

如果这不起作用,你能包括$ drinks_checkbox

If this doesn't work, can you include $drinks_checkbox

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

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