0

Any Help would be appreciated :

I've created a custom page via hook_menu in drupal 7 in which there is a custom form implemented (and additionnal features). I'm trying to implement an Ajax callback on the form submit to refresh dynamically the content part of the page, but i keep having an "error 200" on "system/ajax".

I guess that is because of the route Ajax doesn't find.

How can I implement a custom route for the ajax callback ?

Edit : Here's the code of the module :

function CUSTOM_MODULE_menu() {
  $items = array();

  $items['CUSTOM_ROUTE'] = array(
    'title' => 'TITLE',
    'description' => t('DESSCRIPTION'),
    'page callback' => 'CUSTOM_CALLBACK',
    'access callback' => TRUE,
  );
}

function CUSTOM_CALLBACK(){
  $build = array();

  $build['content'] = array(
    'form_event' => drupal_get_form('MY_CUSTOM_FORM'),
    'list_events' => OTHER_STUFF,
  );

  return theme('custom_theme',$build);
}

function MY_CUSTOM_FORM($form, &$form_state) {
  // FORM ITEMs, CHECKBOXES 
    $form['submit'] = array(
    '#type' => 'button',
    '#value' => t('Search'),
    '#submit' => array(
      'MY_CUSTOM_FORM_submit'
    ),
    '#validate' => array(
      'MY_CUSTOM_FORM_validate'
    ),
    '#ajax' => array(
      'callback' => 'ajax_test_callback',
      'wrapper' => 'result-div',
    )
  );

  $form['results']=array(
    '#prefix' => '<div id="result-div">',
    '#suffix' => '</div>',
    '#weight' => 10001,
  );

  return $form;
}

function ajax_test_callback($form, $form_state) {
  $test ="hello";
  return $test;
}

Here's the error I get :

Code de statut HTTP : 200
Informations de débogage ci-dessous.
Chemin : /system/ajax
StatusText: OK
ResponseText : 

EDIT 2 : In menu.inc, inside menu_execute_active_handler function, I have the following values :

$router_item['page_callback'] = 'ajax_form_callback'
$router_item['page_arguments'] = array();

and the execution stops in the ajax_form_callback function after the list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();

EDIT 3 : After debugging a little bit more the execution, I found out that in ajax_get_form function the $_POST['form_build_id'] was NULL.

  • 200 is not an error, it literally means "OK", that the request has succeeded. You'll probably need to provide a bit more info – Clive Feb 8 '16 at 8:47
  • I've added the code implemented. Hope this helps. – Ismail H Feb 8 '16 at 8:55
  • Change return to print in the CUSTOM_CALLBACK, that should be all you need to do. Just to reiterate, what you think is an "error" isn't - that information is telling you that the response was successful, that there were no errors. – Clive Feb 8 '16 at 8:57
  • I've done it and i'm still getting the same error :( – Ismail H Feb 8 '16 at 9:29
  • 1
    Also, why are you putting this on the submit button instead of the result-div element? youtube.com/watch?v=RSg2uNs8L3g The point of using ajax instead of a custom form submit handler is because you want the form to update and change before the user pushes the submit button. – DeveloperWeeks Feb 8 '16 at 16:55

Your Answer

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

Browse other questions tagged or ask your own question.