I've been trying to use #ajax callbacks on a form. I've got this working with radio buttons, so when I've debugged the form I get the value associated with the radio button as '#value' under 'triggering_element' in $form_state; what I'm struggling with is doing the same thing with a group of checkboxes. The first checkbox checked seems to work correctly, and I can see the correct value, but if I click a second checkbox, the value doesn't come through correctly (triggering element is the field that was returned in the previous #ajax callback for some reason??) and '#value' is a string and not an array as I would have expected. Also $form_state['#values'] doesn't contain the previously selected checkbox or the current checkbox.

How do you correctly add #ajax to an element of type 'checkboxes' and how is it possible to get the value of all checked checkboxes when the ajax callback is fired?

up vote -1 down vote accepted

There is a module that can trigger an ajax callback on a form (content type) maybe that modules solves your issue http://drupal.org/project/conditional_fields.

  • Thanks, I came across this module, but unfortunately it just uses javascript to hide fields and not load them via AJAX. I think this means that if a required field is hidden, the form won't submit unless it has been filled in. If all fields are set to not required, but really they need to be, a custom submit function would need to be created to check for this. Another issues is that when using #states, it's not possible to use a transition t show/hide the fields. I'm going to look into it again though incase I missed something... – jonshot May 28 '13 at 7:08
  • After looking at the module again, it seems to meet my needs so think I'll be using it! – jonshot May 28 '13 at 7:28

I hope this may help you. This example is from Drupal Example module which is creating checkboxes through ajax. Compare your code with it and hopefully will find the culprit.

Drupal Ajaxified Checkboxes example

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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