Checkbox value to be checked

My simple checkbox's code is the following:

<input type="checkbox" name="mycheckbox" value="1" <?php checked(1, get_option('mycheckbox'), true); ?> />

It works fine and values are being stored visually (if I check it, it remains checked and vice versa).

I am trying to set a variable, as a conditional logic, upon its value, but it seems to be failing:

if ( isset($_POST['mycheckbox']) ) {
    $isitchecked = 'yes';
} else {
    $isitchecked = 'no';

Echoing it, returns always 'no' even if it is checked.

1 answer

  • answered 2018-11-08 06:56 John Greco

    FYI, just solved this. The issue was related to the fact that the sample code I used was using WordPress Settings API, so I used this routine, in order to check my box:

      $options = get_option( 'mycheckbox' );
      // If is checked, activate function to display coming soon page
      if( $options['mycheckbox'] == '1' ) { 
          echo 'checked';
      else { 
          echo 'unchecked';