1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Controlling state of checkbox programatically

Discussion in 'PHP' started by HenryCan, Jun 8, 2019.

  1. #1
    I am relatively new to PHP and I'm struggling with how to control checkboxes on my forms.
    I have a group of 6 related checkboxes on a form - there are some other unrelated checkboxes on the form too - and I want all of the six related checkboxes to be checked initially. When the user completes the form, they may very well uncheck one or all of the six checkboxes and that is fine.

    Various edits take place on the various text inputs and text areas on my form and any error will cause the form to be redisplayed with appropriate error messages. When I redisplay the form, I want all of the related checkboxes to be displayed in the proper state. For example, if the first and third checkboxes were unchecked by the user, I want then to be shown unchecked when the form is redisplayed. If they are all unchecked, I want to show them all unchecked when the form is redisplayed.

    I can initialize the checkboxes to checked easily enough by simply including the word "checked" in the original HTML but I'm really having trouble finding a way to programatically turn off (or on) a checkbox without resorting to JQuery or JavaScript, both of which I want to avoid if possible. (I'm trying to make these forms work for users that refuse to enable JavaScript for whatever reason.)

    Can anyone tell me how to turn checkboxes on or off once they're already set?
     
    HenryCan, Jun 8, 2019 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    Try this code.
    Basically, when a group of checkboxes is submitted, then PHP gets the data in an array. You can check this array to see if current array item exists or not, and accordingly check or uncheck checkboxes.

    Put this code in a PHP file, tick a few of the checkboxes, and click the submit button.
    On the next load, those same checkboxes will be checked.

    if(isset($_POST["submit"])){

    // do your checks.


    }

    print '
    <form action="" method="post">
    ';
    for($x=1; $x<5; ++$x){
    if( isset($_POST["cc"][$x]) ){ $type="checked"; }else{ $type=""; }
    echo '<input type="checkbox" name="cc['.$x.']" '.$type.' /> Box '.$x.'<br />';
    }

    print '
    <input type="submit" name="submit" value="submit" />
    </form>
    ';
     
    JEET, Jun 9, 2019 IP
  3. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Thank you for this suggestion, Jeet. It's actually very close to what I'd come up with myself except it has one fundamental problem: I can't figure out how to make sure the checkboxes are initially checked. When I coded it the way you are proposing, the checkboxes were initially unchecked, which is the opposite of what I want. This approach ensures that the correct state of the checkbox is preserved, which is fantastic, but I don't see how to make sure the checkbox is checked when you first see it. The logic needs to be a little more complex, I think. Something like (pseudocode):
    if this is the first time presenting the form, show the checkbox as checked
    else if the user unchecked the checkbox before the most recent Submit, show it as unchecked
    else show it as checked

    I don't know how to write that in PHP. Do you?
     
    HenryCan, Jun 9, 2019 IP
  4. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Thanks for your help, Jeet. I figured out that the best way to proceed was to set a specific value for the unchecked checkboxes during my edits. If the checkbox has THAT value when I redisplay the form to show the errors, then I will display the checkbox unchecked; otherwise, I will show it checked. That works very well.
     
    HenryCan, Jun 9, 2019 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Oh, I get it now. Use this code.
    There is an "else" statement added to the $_POST[submit] check.
    On first load, this will not be set, so we can use that state to tick our checkboxes.

    if(isset($_POST["submit"])){

    // do your checks. of form validation etc

    }else{ //form has not been submitted yet, not even once. This is first load

    for($x=1; $x<5; ++$x){
    $_POST["cc"][$x]='Your_value_here';
    }

    }//submit check ends here



    print '
    <form action="" method="post">
    ';
    for($x=1; $x<5; ++$x){
    if ( isset($_POST["cc"][$x]) ){ $type="checked"; }else{ $type=""; }
    echo '<input type="checkbox" name="cc['.$x.']" '.$type.' /> Box '.$x.'<br />';
    }

    print '
    <input type="submit" name="submit" value="submit" />
    </form>
    ';
     
    JEET, Jun 9, 2019 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    So... relatively new / unaware of how to use HTML either?

    Add the checked attribute, and be done with it. If it EXISTS in the returned form data and you redisplay the form, send them as the appropriate state of having or not having that attribute.

    <input type="checkbox" name="whatever" value="1" checked>

    ... and remember, some UA's won't return the name even when checked if there's no value, and in your $_POST or $_GEt response if the box is unchecked, the index won't even exist so all you REALLY need to go after is isset()

    What question @JEET is answering, seems to have nothing to do with it.
     
    deathshadow, Jun 26, 2019 IP