...

View Full Version : checked boxes not updating correctly



needsomehelp
11-19-2012, 02:18 PM
For some reason no matter how many times I look at the code and make small changes here and there I am still unable to get the boxes to show their correct check or not. The checked boxes are correct when first shown. But if I was to submit the form and forget to confirm by entering my password in another field then the form is shown again with the previously check or not checked boxes. But it always reverts back to what is held in the 'sessions' which is got from the DB when they login.

The echo statements are showing the correct results but the if statements are not working correctly and showing the previously checked or not checked boxes.

What can I possibly be missing here ?



<? echo("<br><br>pmn '".$pmn."'<br><br>mfmn '".$mfmn."'<br><br>ofmn '".$ofmn."'<br><br>micn '".$micn."'<br><br>oicn '".$oicn."'<br><br>"); ?>
<? echo("<br><br>pmn '".$_POST['pmn']."'<br><br>mfmn '".$_POST['mfmn']."'<br><br>ofmn '".$_POST['ofmn']."'<br><br>micn '".$_POST['micn']."'<br><br>oicn '".$_POST['oicn']."'<br><br>"); ?>
<div class="field"> Accept server notification emails for: </div>
<div class="value"> <input name="pmn" value="checked" type="checkbox"<? if(isset($_POST['pmn'])) { if ($pmn == "checked") { echo(" checked=\"checked\""); } } else { if ($_SESSION['user']['pmn'] == 1) { echo(" checked=\"\""); } } ?>> Private Messages </div>
<div class="value"> <input name="mfmn" value="checked" type="checkbox"<? if(isset($_POST['mfmn'])) { if ($mfmn == "checked") { echo(" checked=\"checked\""); } } else { if ($_SESSION['user']['mfmn'] == 1) { echo(" checked=\"\""); } } ?>> Forum Messages (that I started) </div>
<div class="value"> <input name="ofmn" value="checked" type="checkbox"<? if(isset($_POST['ofmn'])) { if ($ofmn == "checked") { echo(" checked=\"checked\""); } } else { if ($_SESSION['user']['ofmn'] == 1) { echo(" checked=\"\""); } } ?>> Forum Messages (other posts where I contributed) </div>
<div class="value"> <input name="micn" value="checked" type="checkbox"<? if(isset($_POST['micn'])) { if ($micn == "checked") { echo(" checked=\"checked\""); } } else { if ($_SESSION['user']['micn'] == 1) { echo(" checked=\"\""); } } ?>> Item Comments (my items) </div>
<div class="value"> <input name="oicn" value="checked" type="checkbox"<? if(isset($_POST['oicn'])) { if ($oicn == "checked") { echo(" checked=\"checked\""); } } else { if ($_SESSION['user']['oicn'] == 1) { echo(" checked=\"\""); } } ?>> Item Comments (other items I contributed) </div>

Fou-Lu
11-19-2012, 03:59 PM
These are pretty hard to read like that. With a single line entry, you'd be better off with a ternary operator. Also, don't use short tags as they are a configuration option, and until the 5.4 is mainstream the short echo with <?= isn't available if short tags are disabled.
I'd make use of a loop to build this, key => value up the name to label, and go from there:


$aPossible = array(
'pmn' => 'Private Messages',
'mfmn' => 'Forum Messages (that I started)',
'ofmn' => 'Forum Messages (other posts where I contributed)',
'micn' => 'Item Comments (my items)',
'oicn' => 'Item Comments (other items I contributed)'
);

foreach ($aPossible AS $sName => $sLabel)
{
$sID = sha1($sName . $sLabel);
$bIsChecked = isset($_POST[$sName]) && $_POST[$sName] == 'checked' ? true : isset($_SESSION['user'][$poss]) ? $_SESSION['user'][$poss] : false;
print('<div class="value">' . PHP_EOL);
printf("\t<label for=\"%s\">%s</label>" . PHP_EOL, $sID, $sLabel);
printf("\t<input type=\"checkbox\" id=\"%s\" name=\"%s\" value=\"checked\"%s/>" . PHP_EOL, $sID, $sName, ($bIsChecked ? ' checked="checked"' : ''));
print('</div>');
}

Use CSS to style appropriately. Try that, I can't test the checked functionality where I am, but it works okay in my head.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum