好的我正在向$ _POST发送数据给自己
我有一些默认选中的复选框。 (本例中为1& 2)
在对表单进行求和时,我想用$ _POST值覆盖这些默认值。
<?php
$syndication_check_1 = 'checked="checked"';
$syndication_check_2 = 'checked="checked"';
$syndication_check_3 = '';
if (isset($_POST['syndication'])) {
//
}
?>
<form name="form" method="post" action="">
<!-- this is what I use for radiobuttons -->
<legend>Sex:</legend>
<input type="radio" name="sex" id="sex-1" value="M" <?php echo (!isset($_POST['sex']) || $_POST['sex'] == "M") ? 'checked="checked"': ''; ?> />
<label for="sex-1"><?= _("Male"); ?></label>
<input type="radio" name="sex" id="sex-2" value="F" <?php echo (isset($_POST['sex']) && $_POST['sex'] == "F") ? 'checked="checked"': ''; ?> />
<label for="sex-2"><?= _("Female"); ?></label>
<!-- now something similar for checkboxes -->
<legend>Syndication:</legend>
<input type="checkbox" name="syndication[]" id="syndication-1" value="yahoo" <?= $syndication_check_1; ?> /> <!-- checked -->
<label for="syndication-1">Yahoo Real Estate</label>
<input type="checkbox" name="syndication[]" id="syndication-2" value="trulia" <?= $syndication_check_2; ?> /> <!-- checked -->
<label for="syndication-2">Trulia.com</label>
<input type="checkbox" name="syndication[]" id="syndication-3" value="zillow" <?= $syndication_check_3; ?> />
<label for="syndication-3">Zillow.com</label>
</form>
答案 0 :(得分:3)
我可能会做这样的事情......
$syndicationCheck = array(
'yahoo' => null,
'trulia' => null,
'zillow' => null
);
if(isset($_POST['syndication'])){
foreach($_POST['syndication'] as $value){
$syndicationCheck[$value] = 'checked';
}
}
<legend>Syndication:</legend>
<input type="checkbox" name="syndication[]" id="syndication-1" value="yahoo" <?= $syndicationCheck['yahoo']; ?> /> <!-- checked -->
<label for="syndication-1">Yahoo Real Estate</label>
<input type="checkbox" name="syndication[]" id="syndication-2" value="trulia" <?= $syndicationCheck['trulia']; ?> /> <!-- checked -->
<label for="syndication-2">Trulia.com</label>
<input type="checkbox" name="syndication[]" id="syndication-3" value="zillow" <?= $syndicationCheck['zillow']; ?> />
<label for="syndication-3">Zillow.com</label>
答案 1 :(得分:1)
通常名称如$ variable1,$ variable2,$ variable3的提示应该是$ variable [0],$ variable [1],$ variable [2]。我现在的内容现在只是稍微方便一点,但如果你扩展到更多的复选框,你会非常感激。
$ syndication_checkboxes将保存每个复选框的名称和检查/未选中状态。初始分配表示“默认”状态,可能会或可能不会被$ _POST覆盖。
<?php
$syndication_checkboxes = array(array('name' => 'Yahoo Real Estate', 'checked' => 1),
array('name' => 'Trulia.com', 'checked' => 1),
array('name' => 'Zillow.com', 'checked' => 0));
if isset($_POST['syndication'])
{
$arr = array_fill(0, count($syndication_checkboxes), 0)
foreach($_POST['syndication'] as $k=>$v) //unfortunately in this case, merges on numeric arrays dont overwrite. This could probably be written nicer as an array_walk or map
$arr[$k] = $v;
foreach($arr as $k=>$v)
$syndication_checkboxes[$k]['checked'] = $v;
}
//Everything up to and including <legend>Syndication...
foreach($syndication_checkboxes as $k=>$v)
{
$checkString = $v['checked'] ? "checked='checked'" : '';
echo "<input type='checkbox' name='syndication[$k]' id='syndication-$k' value='1' $checkString />";
echo "<label for='syndication-$k' >".$v['name']."</label>";
}
echo "</form>"
?>
现在,如果你想添加新的复选框,你只需要将它添加到$ syndication_checkboxes的顶部。这甚至可以来自一个带有'name'和'default-checked'表的数据库,而不是硬编码,如果它变得非常大或者你想用管理工具或其他东西来改变它,那将会很好。