0

I am trying to use a checkbox on Custom Option page by using Setting API. Every thing works fine at back-end and I can update the checkbox states by registering the settings.But when I would like to see the result on the page I am getting same result " The checkbox has not been checked." even so the checkboxed has been checked and updated on the options.php and my custom option page!

Here is what I have for updating and saving the checkbox state:

public  function contactprice_setting(){
    $options = get_option('price_display_options');
    $html = '<input type="checkbox" id="checkbox_contactprice" name="price_display_options[checkbox_contactprice]" value="1"' . checked( 1, $options['checkbox_contactprice'], false ) . '/>';  
    $html .= '<label for="checkbox_contactprice">Uncheck The Text Box</label>';
    echo $html;
    }

As I said this part works fine but when I try to output the result in index.php as:

<?php if( $input_examples['checkbox_contactprice'] == '1' ) { ?>  
    <p>The checkbox checkbox has been checked.</p>  
<?php } else { ?>  
    <p>The checkbox has not been checked.</p>  
<?php } // end if ?>

As I mentioned I am getting the "The checkbox has not been checked." in both checked or on checked situation. Can you please let me know why I am getting this?

  • What is the code that sets $input_examples? Or, perhaps, what is var_dump($input_examples);? – s_ha_dum Aug 18 '13 at 21:43
  • Hi s_ha_dum, using this code <?php var_dump($input_examples);?> in index.php the result is pint out as "null" – Mona Coder Aug 18 '13 at 21:47
1

... using this code in index.php the result is "null"

Since the code referenced is var_dump($input_examples); that means that $input_examples hasn't been set or it out of scope where you are trying to use it. You will need to add ...

$input_examples = get_option('price_display_options'); // maybe not the right key

... to your code before this line:

if( $input_examples['checkbox_contactprice'] == '1' ) {

I am guessing at the option name but you get the idea.

1

Instead of comparing string "1" using == operator you should compare strings using strcmp function as shown in the following code :

<?php if( strcmp( $input_examples['checkbox_contactprice'], '1' ) != 0 ) { ?>  
    <p>The checkbox has been checked.</p>  
<?php } else { ?>  
    <p>The checkbox has not been checked.</p>  
<?php } // end if ?>
  • Hi Vinod, Thanks for your reply but still not working! I mean I am getting same "The checkbox has not been checked." – Mona Coder Aug 18 '13 at 20:40
  • @MonaCoder I have updated my answer. – Vinod Dalvi Aug 18 '13 at 20:48
  • that's wired! still not working! – Mona Coder Aug 18 '13 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.