在提交表单后保留复选框 [英] Keep checkbox checked after form submit

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

问题描述

我有一个页面,其中我显示sql结果作为一个表。我有它,所有的选项默认情况下检查。但是,当用户取消选中某些地点并提交表单时,他/她将无法知道他们正在过滤哪些地理位置,因为所有复选框仍会被选中。我如何才能使它只有被检查的复选框在提交表单后仍保留其值?
谢谢

I have a page where I am displaying sql results as a table. I have it so that all the options are checked on default. However, when the user unchecks some locations and submits the form, he/she won't know what locations they're filtering on because all the checkboxes will still be checked. How can I make it so that only the checkboxes that were checked retain their value after the form is submitted? Thank you

以下是我的页面:

   <?php 

   $start=_GET['start'];
   $end=_GET['end'];

   if(empty($start)){
      $start=date("Ym");
    }
  if(empty($end)){
    $end=date("Ym");
 }


 $places=array();
if(!empty($_GET['cities'])){
    foreach($_GET['cities'] as $loc){
  array_push($places,$loc);
  }
}else{
   $places=('CHI','DET','LA','NYC','DALLAS','SPR','PHI');
}

?>

//html
<form method='GET'>
  START:<input type='text' name='start' value= '<?$start?>'>
   END: <input type='text' name='end' value='<?$end?>'>
 <input type='checkbox' name='cities[]' value='CHI' checked>CHICAGO
 <input type='checkbox' name='cities[]' value='DET' checked>DETROIT
 <input type='checkbox' name='cities[]' value='LA' checked>LAS ANGELES
 <input type='checkbox' name='cities[]' value='NYC' checked>NEW YORK
 <input type='checkbox' name='cities[]' value='DALLAS' checked>DALLAS
 <input type='checkbox' name='cities[]' value='SPR' checked>SPRINGFIELD
 <input type='checkbox' name='cities[]' value='PHI' checked>PHILIDELPHIA
 <input type='submit' value='Filter'>
 </form>

 <?
 $SQL="SELECT NAME,
     ID,
     PHONE,
     EMAIL,
     EVENT,
     LOCATION
 FROM SHOPPERS
 WHERE LOCATION IN ('".implode("', '", $places)."')
 AND EVENT BETWEEN '{$start}' and '{$end}'
 AND ID BETWEEN '25687' AND '28050'
  ";

   //and then fetch array to print out results...
  .....
  ?>


推荐答案

每个< input type =checkbox> 字段,您应该使用PHP来确定 GET 与每个字段关联的变量传递给脚本。在这种情况下,您要将检查属性附加到< input> 字段。您可以通过在字段内使用三元运算符来完成此操作,如下所示:

Rather than hard-coding your "checked" attribute into each of your <input type="checkbox"> fields, you should be using PHP to determine if the GET variable associated with each field was passed to the script. In that case, you want to append the checked attribute to your <input> field. You can accomplish this by using a ternary operator right inside the field, as such:

<input type='checkbox' name='cities[]' value='CHI' <?= (in_array('CHI', $places)) ? 'checked' : ''; ?> >CHICAGO
<input type='checkbox' name='cities[]' value='DET' <?= (in_array('DET', $places)) ? 'checked' : ''; ?> >DETROIT
<input type='checkbox' name='cities[]' value='LA' <?= (in_array('LA', $places)) ? 'checked' : ''; ?> >LAS ANGELES
<input type='checkbox' name='cities[]' value='NYC' <?= (in_array('NYC', $places)) ? 'checked' : ''; ?> >NEW YORK
<input type='checkbox' name='cities[]' value='DALLAS' <?= (in_array('DALLAS', $places)) ? 'checked' : ''; ?> >DALLAS
<input type='checkbox' name='cities[]' value='SPR' <?= (in_array('SPR', $places)) ? 'checked' : ''; ?> >SPRINGFIELD
<input type='checkbox' name='cities[]' value='PHI' <?= (in_array('PHI', $places)) ? 'checked' : ''; ?> >PHILIDELPHIA

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

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