0

I have written the following code

$form['checking'] = array(
    '#type' => 'checkbox',
    '#default_value' => variable_get('checking',0),
    '#title' => t('title'),
    '#required' => TRUE
  );

  return system_settings_form($form);

the checkbox preserve its checked and unchecked state in the form but I can't get value of the checkbox.

I'm printing its value

dsm(variable_get('checking);
print_r(variable_get('checking',0));

Whether a checkbox is checked or unchecked, in both cases it returns me 1

How can I know whether the checkbox is checked or not?

2 Answers 2

0

Have you tried removing the "#required" => TRUE line? If you uncheck the checkbox and the form item is required the form won't validate and your submit callback will not be called.

Also, you should check that your variable isn't overridden in settings.php https://www.drupal.org/node/1525472

1
  • Yes, remove #required => TRUE if the form should be able to be submitted with the checkbox unchecked. If the form shouldn't be able to be submitted without the checkbox being checked, there's no need to get the value as it's always going to be TRUE.
    – user16537
    Commented Nov 14, 2019 at 17:11
-1

hmmm, looks like you're print_r'ing the result from variable_get() - ie. you're unwillingly asking "did variable_get() run correctly?"

try storing the result from variable_get in a variable then print_r'ing that. or use dsm() instead (from the Devel module).

cheers, Luco

2
  • I have also tried with dsm, it is mentioned in the question
    – Ahmad
    Commented Nov 18, 2016 at 13:16
  • oops. well anyway, try storing it in a variable then. Commented Nov 18, 2016 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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