0

I have a submit button in my form and I want to implement it using ajax:

function myid_user_page_form(){ 

   drupal_add_library('system', 'drupal.ajax');

   $form = array();

   $form['id'] = array(
       '#type' => 'fieldset',
       '#title' => t('ID Information'),
       '#prefix' => '<div id="myid_search_id_number_wrapper">',
       '#suffix' => '</div>'
   );  

   $form['submit'] = array(
      '#type' => 'button',
      '#value' => 'Save',   
      '#ajax' => array(
         'event' => 'click',
         'callback' => 'myid_user_page_form_ajax_submit',
         'wrapper' => 'myid_search_id_number_wrapper'
      ), 
      '#attributes' => array(
        'id' => 'myid_save',               
    ),
);

function myid_user_page_form_ajax_submit($form, &$form_state) { 
    watchdog('Wow','Wow');
}

After executing the code above, the page still has it's refresh after it's clickfunctionality. The ajax doesnt work and no Wow message log in my watchdog table from my database, which means the ajax callback function isn't called. Where am I missing?

  • 1
    A few quick things I noticed, which might get you started in the right direction: 1) If that truly is the submit button in the $form['submit'] array, then replace #type => 'button' with #type => 'submit'. Also, you should be returning the $form variable in a hook_form() function. Also, people better at Drupal than myself might know better, but I don't think there's any need to manually include a core Drupal library in your module. – TerryCB May 2 '15 at 16:27
  • Also, setting your own id attribute may be having an effect (not 100% sure though) – 2pha May 2 '15 at 19:10
  • @2pha --> Thanks. Please put your comment in the answer section and I'll accept as an answer. I tried removing myid and it worked fine. – user46175 May 3 '15 at 3:02
1

It is because you are changing the id attribute of the button.
The ajax in Drupal automatically attaches the javascript event listener to the submit, but it expects the button the have the automatically generated id.
Adding or changing the class attribute should not affect it though.

All the suggestions TerryCB made in the comments are also valid.

Your Answer

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