试图从表单中检索复选框值

时间:2013-06-24 02:47:30

标签: php arrays checkbox

我有一个表格,其中包含从表格加载的复选框组。表字段是MySQL TINYINT字段,其中1表示true,0表示false,因此命名为:frequency_sunfrequency_monfrequency_tuefrequency_wed,{{1 },frequency_thufrequency_fri

表单以这种方式设置:

frequency_sat

我怀疑这部分是不必要的和/或笨拙的,但这不是我的主要问题 - 至少不是立即的。 无论如何,正确加载了复选框组。我正在尝试通过将变量设置为 <?php $checked = ($frequency_sun==1) ? 'checked="checked"' : ''; echo "<input type=\"checkbox\"" . $checked . "name='frequency[sun]' value=".$frequency_sun." >Sunday"; $checked = ($frequency_mon==1) ? 'checked="checked"' : ''; echo "<input type=\"checkbox\"" . $checked . "name='frequency[mon]' value=".$frequency_mon." >Monday"; $checked = ($frequency_tue==1) ? 'checked="checked"' : ''; echo "<input type=\"checkbox\"" . $checked . "name='frequency[tue]' value=".$frequency_tue." >Tuesday"; $checked = ($frequency_wed==1) ? 'checked="checked"' : ''; echo "<input type=\"checkbox\"" . $checked . "name='frequency[wed]' value=".$frequency_wed." >Wednesday"; $checked = ($frequency_thu==1) ? 'checked="checked"' : ''; echo "<input type=\"checkbox\"" . $checked . "name='frequency[thu]' value=".$frequency_thu." >Thursday"; $checked = ($frequency_fri==1) ? 'checked="checked"' : ''; echo "<input type=\"checkbox\"" . $checked . "name='frequency[fri]' value=".$frequency_fri." >Friday"; $checked = ($frequency_sat==1) ? 'checked="checked"' : ''; echo "<input type=\"checkbox\"" . $checked . "name='frequency[sat]' value=".$frequency_sat." >Saturday"; ?> 数据来检索表单数据:

$_POST

//然后我设置要在SQL UPDATE中使用的变量,如下所示:

$frequency = $_POST['frequency'];

在选中特定复选框时似乎没有返回值。对于我的测试,我已将表中的$frequency_sun = (isset($frequency['sun']) ? $frequency['sun'] : $frequency_sun); $frequency_mon = (isset($frequency['mon']) ? $frequency['mon'] : $frequency_mon); $frequency_tue = (isset($frequency['tue']) ? $frequency['tue'] : $frequency_tue); $frequency_wed = (isset($frequency['wed']) ? $frequency['wed'] : $frequency_wed); $frequency_thu = (isset($frequency['thu']) ? $frequency['thu'] : $frequency_thu); $frequency_fri = (isset($frequency['fri']) ? $frequency['fri'] : $frequency_fri); $frequency_sat = (isset($frequency['sat']) ? $frequency['sat'] : $frequency_sat); 设置为0,并希望将其设置为1,但我无法弄清楚我哪里出错了。

1 个答案:

答案 0 :(得分:0)

为了保持编码方式,你可以做这样的事情

<?
    if($frequency_sun == 1) $checked_sun = 'checked="checked"';
    if($frequency_mon == 1) $checked_mon = 'checked="checked"';
    if($frequency_tue == 1) $checked_tue = 'checked="checked"';
    if($frequency_wed == 1) $checked_wed = 'checked="checked"';
    if($frequency_thu == 1) $checked_thu = 'checked="checked"';
    if($frequency_fri == 1) $checked_fri = 'checked="checked"';
    if($frequency_sat == 1) $checked_sat = 'checked="checked"';
?>    

    <input type="chexkbox" name='frequency[sun]' value="<?=$frequency_sun?>" <?=$checked_sun?> />Sunday<br />
    <input type="chexkbox" name='frequency[mon]' value="<?=$frequency_mon?>" <?=$checked_mon?> />Monday<br />
    <input type="chexkbox" name='frequency[tue]' value="<?=$frequency_tue?>" <?=$checked_tue?> />Tuesday<br />
    <input type="chexkbox" name='frequency[wed]' value="<?=$frequency_wed?>" <?=$checked_wed?> />Wednesday<br />
    <input type="chexkbox" name='frequency[thu]' value="<?=$frequency_thu?>" <?=$checked_thu?> />Thursday<br />
    <input type="chexkbox" name='frequency[fri]' value="<?=$frequency_fri?>" <?=$checked_fri?> />Friday<br />
    <input type="chexkbox" name='frequency[sat]' value="<?=$frequency_sat?>" <?=$checked_sat?> />Saturday<br />

接下来,您需要从频率数组中获取值

$frequency = $_POST['frequency'];
$frequency_sun = $frequency['sun'];
"UPDATE yourtable SET sunday = '$frequency_sun', ..... WHERE something = 'value'";

然后选择

"SELECT * FROM yourtable WHERE something = 'value'"

最后你的价值观

$frequency_sun = $query_result['sunday'];
.
.
.

当然,您需要在表单之前从数据库中获取值。