Support

Account

Home Forums General Issues If a checkbox value is checked

Solving

If a checkbox value is checked

  • I have a checkbox field with three options. I’m trying to use a PHP if statement to show a particular value if one of them is checked but it won’t work. I’d previously done this with only 1 option simply checking if it’s ‘true’ but can’t do this now I’m using three.

    The current piece of code (not working) is:

    <?php
    if ( in_array( 'in_stock', get_field('stock') ) ) 
    { ?>   
    
    STUFF HERE
    
    <?php } ?>
  • Hello 🙂
    First, try to output the array content, to check if the value is called correctly:

    
    echo '<pre>';
    var_dump(get_field('stock'));
    echo '</pre>';
    

    Paste here the results 😉

  • Thanks, Roberto. By using the following code (pasting it to check I’ve got it right), I see no change — it’s not showing anything for this particular field.

    <?php
    if ( in_array( 'in_stock', get_field('stock') ) ) 
    { ?>   echo '<pre>';
    var_dump(get_field('stock'));
    echo '</pre>';
    <?php } ?>
  • Don’t put it inside the IF, call it outside.

  • OK, it comes out looking like this:

    Screengrab

  • You should write here only the results of var_dump(get_field(‘stock’)).

    What it output? Array? empty array?

  • Isn’t it so that when jut one checkbox has been checked the return is not an array but rather just a string.. So if you know that your user will always just check one (if so Id use radiobuttons instead) you should just check it like a regular string..

    
    <?php if ('in_stock' == get_field('stock')) { ?>   
    
    STUFF HERE
    
    <?php } ?>
    
  • Ok, you can merge the checks:

    
    <?php
    if( in_array( 'in_stock', get_field('stock') ) or 'in_stock' == get_field('stock') ) {
    ?>   
    
    STUFF HERE
    
    <?php
    }
    ?>
    
  • Thanks both — after reading Jonathan’s comment, I realise that radio buttons are actually better in this case as the user will only be checking one option.

    I’ve tried both the pieces of code suggested and neither outputs anything.

  • Okay, It’d be much easier to help you if we could actually see the whole code in context 🙂

  • I have radio buttons set up with three options:

    Forthcoming
    In Stock
    Out of Stock

    These are set up to run on a custom post type, ‘Book’

    I want to output something different depending on which option is checked.

    This all takes place within the loop.

    I don’t have any code relevant to the issue to show I’m afraid. I had previously used a single checkbox and called it as follows:

    <?php if ( get_post_meta($post->ID, 'paperback', true) ) : ?>
    	Paperback
    <?php endif; ?>

    This worked fine, but I don’t know how to query the custom field AND the option selected, which is the problem here.

    I hope that’s a bit more clear, sorry I can’t provide more!

  • 
    <?php 
    if(in_array('In Stock', get_field('stock'))){
    
    }else if(in_array('Forthcoming', get_field('stock'))){
    
    }else if{in_array('Out of Stock', get_field('stock')){
    
    }else{
    No selected
    }
    
    ?>
    
    

    This should work then according to http://www.advancedcustomfields.com/resources/field-types/checkbox/
    which I assume works the same as radiobuttons..

  • Thanks Jonathan, that seems logical but unfortunately it’s just crashing the site (white screen).

  • the do this inside the loop but outside any if statements:

    
    print_r(get_field('stock'));
    

    And post the result here

  • Simply outputs that into the page:

    print_r(get_field('stock'));

  • ?
    But have you place it inside “<?php ….. ?>” ?

  • As roberto says.. you need to put this inside php tags for the code to run.. or it will just be treated as a regular string!

    This applies to everything that’s not HTML or a string.

  • Silly me. Wrapped as follows:

    <?php print_r(get_field('stock')); ?>

    …it doesn’t change anything on the page at all.

  • Please, attach the php file here so we can examine it.

  • OK, here it is — I’ve tried many different configurations and nothing works so something is clashing I guess.

    Be warned, this code is probably very messy — it’s a couple of years old, hence why I’m revisiting it now to tidy it up!

    Code

  • Good morning,

    So I’ve taken a look at your file and I can’t even find any reference to that which you are doing? Where in the code is this supposed to happen since there’s not anything there?

    In any case. The markup was chaos so I took the liberty of cleaning it up a bit for you with proper indents and a missing end div-tag.
    http://pastebin.com/PdJ6GAb2

  • Thanks Jonathan — and for tidying it up, that’s much appreciated. The markup is terribly messy at the moment and that’s one of the reasons I’m revisiting it now as I know a little more now than I did when this was put together.

    I didn’t include the radio button snippet as much of the code on the page will be changing over the coming weeks (I don’t envisage this causing a problem), but it will be going, with some other stuff, in place of the ‘details binding’ section that appears at present.

    Thanks

  • Here was a simple solution that I came up with for a single checkbox. My use is to declare a title to be echoed as an <h2> or <h3> tag in the html. I post this for anyone else looking for an easy way to use a single checkbox.

    
    if($h3 = get_sub_field('h3_option')){
    
                                            echo "<h3>{$title}</h3>";
                                    } else {
    
                                            echo "<h2>{$title}</h2>";
                                    }
    

    I tried the “basic logic” option in on this page: https://www.advancedcustomfields.com/resources/checkbox/

    <?php 
    
    $selected = get_field('field_name');
    
    if( in_array('red', $selected) ) {
    	
    	echo 'Red was selected!';
    	
    }
    
    ?>

    But I found it unnecessary for my single selection.

    Do note the “get_field” and “get_sub_field” difference in the 2 examples.

    I am using sub_field because we are using the flex content. Big difference for those just getting to know this plugin.

  • Hi @decay

    Thank you for the examples. I want to note that you’re setting a $h3 variable inside the if statement that’s not being used in the example.

    So for anyone looking to use this you do not need that. Here’s a cleaned up example:

    
    if( get_sub_field( 'fieldname/fieldkey' ) ){
        echo sprintf( '<h3>%s</h3>', $title );
        
    } else {
    	echo sprintf( '<h2>%s</h2>', $title );
    }
    
    

    And a shorthand version:
    echo ( get_sub_field( 'fieldname/fieldkey' ) ? sprintf( '<h3>%s</h3>', $title ) : sprintf( '<h2>%s</h2>', $title ) );

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

The topic ‘If a checkbox value is checked’ is closed to new replies.