2010-11-30 117 views
-1

好吧我正在用$ _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> 
+2

你的問題是什麼? – Beaker 2010-11-30 08:21:20

+0

是的,它不清楚 – 2010-11-30 08:21:55

+0

我怎樣才能用$ _POST數據值覆蓋我的默認值? – FFish 2010-11-30 08:49:34

回答

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名,$變量2,$ variable3是暗示,它應該是$變量[0],$變量[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'表,而不是硬編碼,如果它變得很大或者你想用管理工具或其他東西改變它,這將是很好的。