Conditional display of single checkbox results not working

Home Forums WPAlchemy Conditional display of single checkbox results not working

This topic contains 6 replies, has 1 voice, and was last updated by  cosmocanuck 11 months, 4 weeks ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2608

    cosmocanuck
    Participant

    Hi all! I have a single checkbox next to an attribute which is “yes or no”. If it’s checked, i want ‘Yes’ to be displayed in my template; if it’s not checked, I want to display “No”. Simple, right? Not so much, it seems.

    My code for the custom metabox (including the table cell it’s wrapped in):

    the_field('night_skiing_checkbox'); ?>
    <input type="checkbox" name="the_name(); ?>" value="Yes"the_checkbox_state('Yes'); ?>/>

    …and for the display in the template:

    the_value('night_skiing_checkbox') == 'Yes') {
    echo "Yes";
    }
    else {
    echo "No";
    }
    ?>

    If it’s checked, I get a nice “Yes” displayed. But if it’s unchecked, it displays “YesNo”!

    Just when you think you’re past the newbie stuff…. sigh…. hope someone can point out my (simple, I’m sure) error. Thanks!

    #2609

    cosmocanuck
    Participant

    Sorry, guess I need to leave the php tags out of things here.

    Metabox code:


    $mb->the_field('night_skiing_checkbox'); ?>
    <input type="checkbox" name="the_name(); ?>" value="Yes"the_checkbox_state('Yes');

    Template code:

    if ($custom_metabox->the_value('night_skiing_checkbox') == 'Yes') {
    echo "Yes";
    }
    else {
    echo "No";
    }

    #2610

    cosmocanuck
    Participant

    OK, I realized I should use if ($_POST['night_skiing_checkbox'] == 'Yes') for my test. BUT… turns out that’s only half the problem.

    The checkbox re-checks itself after I hit “Update” on my post. It won’t stay unchecked. I’m adding it to the metabox with:

    $mb->the_field('night_skiing_checkbox'); ?>
    <input type="checkbox" name="the_name(); ?>" value="Yes"the_checkbox_state('Yes');

    Which seems to be precisely how the single-checkbox demo is done. Where have I messed up?

    I realize I’m a noob swimming with the code wizards here, but would sure appreciate any hints. Thanks!

    #2611

    cosmocanuck
    Participant

    I can’t delete a post so I’m replacing the text with this… I don’t think my last addition was on base at all. Sorry!

    #2612

    cosmocanuck
    Participant

    I had duplicate checkbox field ID’s – hadn’t yet modified one of them. Sigh… thus the weirdness. Thanks to the community for ignoring my posts while I self-solved.

    #2613

    cosmocanuck
    Participant

    OK, the checkboxes seem fine, but I still can’t get the basic logic to work for displaying a result based on whether it’s checked or not.

    Here’s the test link where I’m displaying the info:

    http://escape2ski.adamabrams.com/?p=18

    I added this just prior, to test:

    The value is $custom_metabox->the_value('night_skiing_checkbox');. (removed the php wrapper here just in case, but it does work).

    If it’s unchecked, it says “The value is .” If checked, “The value is Yes.” Just as I expected. But below that, you can see that my conditional code always brings up a “No”.

    Hope someone can indicate why it’s not working.

    And sorry for the bad netiquette of multi-posts before I had really figured out the problem.

    #2627

    cosmocanuck
    Participant

    OK, I was using “the_value” and should have been using “get_the_value” instead. Nothing to do with my logic for the display, as I was thinking. Sigh…

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.