I am trying to apply the #options value of a checkboxes in a form inside a Ajax callback.

Even after using $form['tags']['table']['#options'] = $final; the value in the form is not changing.

I have tried printing the #options value in the watchdog and the value seems to have changed. But it is not getting reflecting on the form page.

The form field which needs to be updated

$form['tags']['ask_button'] = array( '#type' => 'button', '#value' => t('Next'), '#ajax' => array( 'callback' => 'ajax_tag_callback', 'wrapper' => 'replace_tagfield_div', ), ); $form['tags']['question_tags'] = array( '#title' => t("Tags"), '#type' => 'textfield', '#prefix' => '<div id="replace_tagfield_div">', ); $form['tags']['table'] = array( '#type' => 'checkboxes', '#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))), '#suffix' => '</div>', );

The Ajax callback

function ajax_tag_callback($form, &$form_state) { \\some code\\ $final = drupal_map_assoc(array(t('SAT'), t('ACT'))); $form['tags']['table']['#options'] = $final; $form['tags']['question_tags']['#value'] = $tag_names; \\returned the form here return $form['tags']; } Even after returning the form field, my form is not getting updated. Please suggest me a solution..

  • I have tried using tableselect, but it didnt work somehow – Bej Jan 4 '17 at 11:34

* Update using new code *

Form definition:

$form['tags']['ask_button'] = array(
  '#type' => 'button',
  '#value' => t('Next'),
  '#ajax' => array(
    'callback' => 'ajax_tag_callback',
    'wrapper' => 'replace_tagfield_div',
$form['tags']['values'] = array
  '#prefix' => '<div id="replace_tagfield_div">',
  '#suffix' => '</div>',
  $final = drupal_map_assoc(array(t('SAT'), t('ACT')));
  $form['tags']['values']['question_tags'] = array(
    '#title' => t("Tags"),
    '#type' => 'textfield',
    '#value' => $tag_names, // Note - you didn't show how this is created
  $form['tags']['values']['table'] = array(
    '#type' => 'checkboxes',
    '#options' => $final,
  $form['tags']['values']['question_tags'] = array(
    '#title' => t("Tags"),
    '#type' => 'textfield',
  $form['tags']['values']['table'] = array(
    '#type' => 'checkboxes',
    '#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),

And your ajax callback will look like this:

function ajax_tag_callback($form, &$form_state) {
  return $form['tags']['values'];

* Original response *

Not enough information provided to say for sure what your problem is, but it's likely one of these:

Problem 1:

You are making changes to the form in the ajax callback. You cannot do this. Here is how the form API works.

1) Form is generated

2) Form is cached on server

3) Form is sent to user

4) User initiates some ajax

5) Values are sent to server

6) Form is rebuilt, with submitted values

7) New form is cached on server

8) Form is sent to ajax callback

9) Relevant values are returned from ajax callback

10) Drupal sends data to browser, and the new parts of the form are injected into the page

11) User submits form

12) Drupal checks submitted values to the cached form, to see if the received values match the cached form, giving an error if they don't. This is the major strength of the Form API.

Step 7 and 8 are the key points here - the form has already been cached before it is sent to the ajax callback. So any changes you make won't be part of the cache causing errors.

Problem 2

You don't have a #prefix set on $form['tags']. You have only set the suffix. You need to provide a wrapper with your #ajax definition (which you didn't show us), so maybe the wrapper you have provided does not exist.

  • problem1- I have changed other fields of the form-api lik textfield and button which are getting altered correctly. Only this checkboxes field is not get updated.(P.S. i have also tried tableselect instead of checkboxes which also didnt work) problem2 - the #prefix of the wrapper is set above these fields, i thought it would be unnecessary to add that part of the code. – Bej Jan 5 '17 at 5:40
  • Problem 1 will only become a problem when you submit your form. You'll get an 'illegal choice detected' error. For your other comment - you haven't provided enough code to be able to tell you the problem with your code. – Jaypan Jan 5 '17 at 5:41
  • i have added the code. please give a solution – Bej Jan 5 '17 at 5:51
  • I updated my response with the proper way to do this. But note that your changes don't seem to make sense - you didn't set $tag_names anywhere, and the table values are exactly the same whether it's in the original response or the ajax callback. – Jaypan Jan 5 '17 at 5:59
  • Thankyou so much. Updating the form inside the ajax was the problem . – Bej Jan 5 '17 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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