Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    434
    Thanks
    7
    Thanked 3 Times in 3 Posts

    checked boxes not updating correctly

    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 ?

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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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:
    PHP Code:
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •