Posted by Tiuya on February 28, 2010 at 8:01am
Hello,
I'm having difficulty testing whether a checkbox has been checked when dealing with a checkboxes field type (not a single checkbox).
Lets say I have and administrative form with this:
[] Grapes
[] Oranges
[] Apples
[] Poops
[] Bananas
[] Grapefruit
and I want to display certain content on another page only if Grapes has been checked. How would I write if Grapes has been checked on this checklist, do this?
Thanks,
Tiuya
Comments
r u using cck??
r u using cck??
Nope, it's a checkboxes form
Nope, it's a checkboxes form element coded into the admin form of the module.
The admin will find it in admin/fruits and on node/1, I want to post 'The admin's favorite fruit is Grapes!' if they selected Grapes. Well, that's not exactly it but that's the idea.
Your best bet is to use
There's probably a better solution, but this works:
Pull the checkbox array, do an array_intersect with them, then use in_array() to see if it's there.
For example:
<?php
$checkbox_results = *pull the values from the database or wherever*;
$checked = array_intersect(array_keys($checkbox_results), array_values($checkbox_results));
if (in_array('Grapes', $checked)) { echo 'Yay, Grapes!'; }
else { echo 'Boo, no grapes!'; }
?>
I'm not stalking you, btw. I'm just working on a module that has a lot of similar features, I guess.