简体   繁体   中英

Keep checkbox checked after form submit

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

Here's my page so far:

   <?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...
  .....
  ?>

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

you could send the parameter which checkbox is checked in GET request, and check it in your page eg

if ($_GET['param1']==1) // checked
{
?>
<input type='checkbox' name='cities[]' value='CHI' checked>CHICAGO
<?
else
?>
<input type='checkbox' name='cities[]' value='CHI' >CHICAGO
?>
<?php
$citiesGroup = array();
$citiesGroup['CHI'] = 'CHICAGO';
$citiesGroup['DET'] = 'DETROIT';
$citiesGroup['LA'] = 'LAS ANGELES';
$citiesGroup['NYC'] = 'NEW YORK';
$citiesGroup['DALLAS'] = 'DALLAS';
$citiesGroup['SPR'] = 'SPRINGFIELD';
$citiesGroup['PHI'] = 'PHILIDELPHIA';

$citiesChecked = array();
if (!empty($_GET['cities'])) {
    foreach ($_GET['cities'] as $cityCode) {
        $citiesChecked[] = $cityCode;       
    }
}
else {
    foreach ($citiesGroup as $key => $value) {
        $citiesChecked[] = $cityCode;       
    }
}
?>
<?php
foreach ($citiesGroup as $cityId => $cityName) {
    $chekced = in_array($cityId, $citiesChecked) ? 'checked="checked"' : '';
?>
<input type='checkbox' name='cities[]' value='<?php echo $cityId;?>' <?php echo $chekced;?>><?php echo $cityName;?>
<?php
}
?>

Get all checkboxed HTML in array and loop over it.

Above is the working code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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