2

I have a Checkbox with different values. When a user change the Checkbox I will trigger the Drupal-Function field_attach_update http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_update/7

I know how I check the checkbox-change with jQuery but how can I trigger the Drupal-Function then?

1 Answer 1

1

You'll want to check out the Form API ajax options. Specifically I think you'll want to define an ajax['callback'] function that calls field_attach_update.

<?php
function my_form_func($form, $form_state) {
  $my_checkbox_val = isset($form_state['values']['my_checkbox']) ? $form_state['values']['my_checkbox'] : NULL;
  $form['my_checkbox'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check me'),
    '#default_value' => $my_checkbox_val,
    '#return_value' => $nid, // Assuming you are working with a node, but could be any entity
    '#ajax' => array(
      'callback' => 'my_form_field_update_func',
      'event' => 'click',
    ),
  );
  return $form;
}

function my_form_field_update_func($form, $form_state) {
  if (isset($form_state['values']['my_checkbox'])) {
    $node = node_load($form_state['values']['my_checkbox']);
    field_attach_update('node', $node);
  }
  return $form['my_checkbox'];
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.