1

I am trying to code Drupal Ajax forms in Drupal pages (Drupal Content type: Basic page). However, it seems like the callback does not work, because when I remove the callback function nothing changes, and when I paste it back nothing changes.

Here is my code:

$the_ajax3_form = drupal_get_form('ajax_example_simplestt');
print drupal_render($the_ajax3_form);

function ajax_example_simplestt($form, &$form_state) {
  $form = array();
  $form['changethis'] = array(
    '#title' => t("Choose something and explain why"),
    '#type' => 'select',
    '#options' => array(
      'one' => 'one',
      'two' => 'two',
      'three' => 'three',
    ),
    '#ajax' => array(
      'callback' => 'ajax_example_simplestt_callback',
      'wrapper' => 'replace_textfield_div',
    ),
  );

  $form['replace_textfield'] = array(
    '#type' => 'textfield',
    '#prefix' => '<div id="replace_textfield_div">',
    '#suffix' => '</div>',
  );

  if (!empty($form_state['values']['changethis'])) {
    $form['replace_textfield']['#description'] = t("Say why you chose '@value'", array('@value' => $form_state['values']['changethis']));
  }
  return $form;
}

function ajax_example_simplestt_callback($form, $form_state) {
  return $form['replace_textfield'];
}
1

Your AJAX function callback is misspelled. This would be why it is not getting called.

Change ajax_example_simplest_callback to ajax_example_simplestt_callback.

Whichever spelling is correct, simplest or simplestt... Not knowing your context I do not know which it should be. But yeah, the function in the #ajax property is not defined in the context of this question.

| improve this answer | |
  • thank you for your reply. I have edited the code and corrected the misspelled function for ajax callback but i till have the same problem. Nothing changes. I also cleared the cache and refreshed the page, nothing. Can try to run the code if you are able. – Ngatia Frankline Jul 6 '16 at 11:21
  • The next thing to do would be to put an exit() (not recommended), devel (for dpm or dsm), or use a debugger like xdebug (highly recommended) to step through the code, and ensure that the callback is being hit. – Kevin Jul 6 '16 at 13:12
  • Hi Kevin, i have debugged the form using devel and everything is okay. However, i used firebug to debug the response to the server and realised that the function does not return data. More help will be appreciated... – Ngatia Frankline Jul 7 '16 at 9:45
  • Since this looks like the same example given in the examples module, I would assume that it is working (given no typos) and more in how it is being shown on the page. What is the result if you move this to being a menu item callback instead of the first two lines of code? – Kevin Jul 7 '16 at 13:21
  • Hi Kevin, let me work on that...ill get back to you soon. – Ngatia Frankline Jul 7 '16 at 23:41

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.