开发者

PHP $_POST self: defaults for checkboxes

OK I am sending data with $_POST to itself.

I have some checkboxes that I have checked by default. (1 & 2 in this example)

When the form is sumbitted I want to override these default's with the $_POST value.

<?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" v开发者_C百科alue="zillow" <?= $syndication_check_3; ?> />
    <label for="syndication-3">Zillow.com</label>
</form>


Id probably do something like this...

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


Often names like $variable1, $variable2, $variable3 are hints that it should be $variable[0], $variable[1], $variable[2]. What I have below is only slightly more convenient now, but you'll really appreciate this if you extend to more checkboxes.

$syndication_checkboxes will holds the names and check/unchecked status of each checkbox. The initial assignment represents "default" status which may or may not be overridden by $_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>"
?>

Now if you want to add new checkboxes all you have to do is add it to $syndication_checkboxes assignment up top. This could even come from a database with tables for 'name' and 'default-checked' instead of being hardcoded which will be nice if it gets really big or you want to change it with an admin tool or something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜