0

I have a form which has a checkboxes field.

This field has an ajax callback attached

$form['checkboxes'] = array(
  '#type' => 'checkboxes',
  '#title' => 'test',
  '#ajax' => array(
    'callback' => 'mymodule_ajax_callback',
    'wrapper' => 'test-wrapper',
    'method' => 'replace'
  );
);

function mymodule_ajax_callback(&form, &$form_state) {
  return $form['another-field'];
}

When I check a checkbox, the ajax callback is executed. Then when I try to submit the form through form submit button, it reloads the same page with values in the fields, but the form is not submitted.

I have tried $form_state['rebuild'] = TRUE in the ajax callback also but it did not help.

I have added the #name and #id properties also to the submit button but no luck.

Is there any other solution which I can try?

  • Could you please post the code for the submit button? – Jdrupal Aug 28 at 7:06
  • The submit function is not calling – Ahmad Aug 28 at 7:22
  • @Jdrupal When I submit the form without checking a checkbox in that field, the form gets submitted successfully and the submit function calls – Ahmad Aug 28 at 7:23
  • Event the validation does not work after ajax callback executed. – Ahmad Aug 28 at 7:25
0

I think the problem is that you only return the another-field in the ajax callback.

You probably have to make a wrapper for the submit and the another-field.

So you willl have:

$form['ajax_wrapper'] = array(
  'submit' => array(
    // Submit button...
  ),
  'another-field' => array(
    // Another field...
  ),
);

And then return $form['ajax_wrapper'] in your ajax callback. Make sure to use $form_state['rebuild'] = TRUE;

  • This will work by I think this is not the problem. Because it is already working for one language and for other languages it is not working. – Ahmad Aug 28 at 8:39
  • What do you mean by language? – Jdrupal Aug 28 at 8:40
  • When you return only one field in your ajax callback, then the submit button "is not a part of the form" anymore, if you understand what I mean? You have to return all elements that you want to be in the form after you have checked the checkboxes. – Jdrupal Aug 28 at 8:42

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.