0

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...
  .....
  ?>
user3266259
  • 369
  • 3
  • 8
  • 22

3 Answers3

3

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
Ben Harold
  • 6,242
  • 6
  • 47
  • 71
  • When I did this, it keeps giving me an error, saying unexpected ';' – user3266259 Dec 02 '14 at 16:19
  • Sorry, I was missing a closing parenthesis around the first argument of the ternary operator. I've updated the code now; hopefully it should work. – Ben Harold Dec 02 '14 at 16:34
  • Thank you! It was giving me an error at first because the array should be listed as the second argument not the first, but when I fixed that it works :) – user3266259 Dec 05 '14 at 14:53
1

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

if ($_GET['param1']==1) // checked
{
?>
<input type='checkbox' name='cities[]' value='CHI' checked>CHICAGO
<?
else
?>
<input type='checkbox' name='cities[]' value='CHI' >CHICAGO
?>
Aldy syahdeini
  • 349
  • 1
  • 4
  • 16
0
<?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.

Pupil
  • 23,834
  • 6
  • 44
  • 66