Skip Navigation

[Resolved] Conditional Display Using Checkbox Value and PHP

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by julieP 4 months ago.

Author
Posts
#2065421

I have a Checkboxes field with options 1 and 2 and set to save nothing if unchecked. I can display the checked values using types_render_field() but my code to display content based on the value(s) saved for the field isn't generating the correct output. This is my code:-

$option1 = 'A';
                    $option2 = 'B';  
                    $values = types_render_field( "apply-to-list", array("post_id" => '20', "separator" => ", " ) );                     
                    $values_arr = explode(', ', $values);
                     
                    if (in_array($option1, $values_arr) && in_array($option2, $values_arr)) {
                        echo '<p>A and B</p>';
                    } else if (in_array($option1, $values_arr) && !in_array($option2, $values_arr)) {
                        echo '<p>A</p>';
                    } else if (!in_array($option1, $values_arr) && in_array($option2, $values_arr)) {
                        echo '<p>B</p>';
                    } else {
                        echo '<p>Neither A nor B</p>';
                    }

The uploaded image shows the value in the database is A but the output on the page is "Neither A nor B".

Any ideas please?

#2065425

I discovered the values to check against are the checkbox title not the value saved to the database