我有一个页面,我将sql结果显示为表格。我有它,以便默认选中所有选项。但是,当用户取消选中某些位置并提交表单时,他/她不知道他们要过滤的位置,因为仍会检查所有复选框。如何才能使表单提交后只有选中的复选框保留其值? 谢谢
到目前为止,这是我的页面:
<?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...
.....
?>
答案 0 :(得分:3)
您应该使用PHP来确定与每个字段关联的<input type="checkbox">
变量是否已传递给脚本,而不是将“已检查”属性硬编码到每个GET
字段中。在这种情况下,您希望将checked
属性附加到<input>
字段。您可以通过在字段内使用三元运算符来实现此目的,如下所示:
<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
答案 1 :(得分:1)
您可以在GET请求中发送选中复选框的参数, 并在您的页面中查看它 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
?>
答案 2 :(得分: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
}
?>
获取数组中的所有复选框HTML并循环遍历它。
以上是工作代码。