【问题标题】:PHP $_POST self: defaults for checkboxesPHP $_POST self:复选框的默认值
【发布时间】:2010-11-30 22:21:31
【问题描述】:

好的,我正在使用 $_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>

【问题讨论】:

  • 如何使用 $_POST 数据值覆盖我的默认值?

标签: php post checkbox


【解决方案1】:

我可能会做这样的事情......

$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>

【讨论】:

  • 只是一个更正.. if (isset($_POST['syndication']) { 需要添加另一个“)”
【解决方案2】:

通常像 $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 分配中。这甚至可以来自包含“名称”和“默认检查”表的数据库,而不是硬编码,如果它变得非常大或者您想使用管理工具或其他工具更改它,这将是很好的。

【讨论】:

  • 没问题。我不知道您对该网站的意图,但我认为这确实可以节省一些时间和将来对这些复选框 HTML 元素进行重复的硬编码。我觉得 if 块可以更整洁(也许你可以用 lambdas 和 array_walk 或 array_map 做一些更好的事情?),我还没有测试过它,但即使它错了,我想你明白我想要什么。你试过了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
相关资源
最近更新 更多