0

This is my ajax callback function

function myid_search_idnumber($form, $form_state)
{
    return $form['id'];
}

It updates the values in $form['id']. But Because I wanted to call a javascript function as well as update the form, I added a few codes:

function myid_search_idnumber($form, $form_state)
{
    $commands[] = ajax_command_invoke(NULL, 'myid_update_id_number'); 
    $page = array( '#type' => 'ajax', '#commands' => $commands, ); 
    ajax_deliver($page); 

    return $form['id'];
}

The three lines of code added above, calls a javascript function.

$.fn.myid_update_id_number = function(){   
    alert('Wew');         
}

Performing those only calls the javascript function but does not update anymore my form. How will I call my javascript and at the same time update my form?

1

Just to ad to the above answer by Ollie.
To return form['id'] too (same as the default behavior), you can use ajax_command_replace

function myid_search_idnumber($form, $form_state){
    $commands[] = ajax_command_replace(NULL, render($form['id']));
    $commands[] = ajax_command_invoke(NULL, 'myid_update_id_number', array($form['id'])); 
    $page = array( 
         '#type' => 'ajax', 
         '#commands' => $commands, 
      ); 
    ajax_deliver($page);
  }
0

ajax_deliver() calls ajax_footer(), which basically terminates the request, and exits. Hence your return $form['id'] isn't called.

Why don't you simply add the form_id as a parmeter to ajax_command_invoke? Haven't tested it, but it should work. Have a look at the documentation for the ajax_command_invoke function in /includes/ajax.inc around line 1185.

function myid_search_idnumber($form, $form_state){ $commands[] = ajax_command_invoke(NULL, 'myid_update_id_number', array($form['id'])); $page = array( '#type' => 'ajax', '#commands' => $commands, ); ajax_deliver($page); }

Your Answer

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