2010-11-30 3 views
-1

OK Ich sende Daten mit $ _POST an sich.
Ich habe einige Kontrollkästchen, die ich standardmäßig aktiviert habe. (1 & 2 in diesem Beispiel)
Wenn das Formular gesendet wird, möchte ich diese Standardwerte mit dem $ _POST-Wert überschreiben.

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

Was ist Ihre Frage? – Beaker

+0

Ja, es ist nicht klar –

+0

Wie kann ich meine Standardwerte mit den $ _POST Datenwerten überschreiben? – FFish

Antwort

3

Id tun wahrscheinlich so etwas wie dieses ...

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

da gehen wir ... vielen Dank! – FFish

+1

nur eine Korrektur .. if (isset ($ _ POST ['Syndication']) {muss ein weiteres hinzufügen ")" – FFish

1

Oft Namen wie $ variable1, $ variable2, $ Variable3 Hinweise sind, dass es Variable $ sein sollte [0], $ variable [1 ], $ Variable [2]. Was ich unten habe, ist jetzt nur etwas bequemer, aber Sie werden das wirklich zu schätzen wissen, wenn Sie mehr Checkboxen haben.

$ syndication_checkboxes enthält die Namen und den Check/Unchecked-Status jedes Kontrollkästchens. Die anfängliche Zuweisung stellt den "Standard" -Status dar, der von $ _POST überschrieben werden kann oder nicht.

<?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>" 
?> 

Nun, wenn Sie neue Kontrollkästchen hinzufügen möchten alles, was Sie tun müssen, ist es Zuweisung bis oben syndication_checkboxes zu $ ​​hinzufügen. Dies könnte sogar von einer Datenbank mit Tabellen für 'Name' und 'Standard-geprüft' kommen, anstatt fest codiert zu sein, was nett ist, wenn es wirklich groß wird oder Sie es mit einem Admin-Tool oder etwas ändern wollen.

+0

Vielen Dank Jon für Ihre Idee! – FFish

+0

kein Problem. Ich kenne Ihre Absichten mit der Seite nicht, aber ich denke, das könnte wirklich einige Zeit und repetitive Hard-Codierung dieser Checkbox HTML-Elemente in der Zukunft speichern. Ich fühle mich wie der if Block könnte besser sein (vielleicht können Sie etwas besser mit lambdas und array_walk oder array_map tun?), und ich habe es nicht getestet, aber selbst wenn es falsch ist denke ich, Sie sehen, was ich will. Hast Du es versucht? –