How to add AJAX call back from another AJAX result in a form

by Ak_mana   Last Updated July 17, 2017 10:07 AM

I want to use 2 AJAX call back within a form. The scenario is like I am having a text field and a Search button which is defined in the form alter function. After entering a text and clicking on the Search button, will display some matching strings with some other information from database as an HTML in ul li a format.

I had made each string as a form element link and displaying under the search field. Now, I need to add AJAX callback to this link and after clicking on any string, the whole HTML content has to be removed and some other new text fields should be displayed in the form and the selected string should be placed on the text field.

Please see below codes.


  $form['searchbutton'] = array(
      '#type' => 'button',
      '#value'=> t('Search'),
      '#id'   => 'search_button',
      '#ajax' =>  array(
          'callback' => 'ajax_search_callback',
          'wrapper' => 'search-list-wrapper',
          'method' => 'replace',
          'effect' => 'fade',
          'progress' => array('type' => 'none'),

Below code is the first AJAX call on button click

     * AJAX call back 
function ajax_search_callback($form, $form_state) {

//getting matching list from database with respect to search text
// $search_results

 $form['link-lists'] = array(
        '#type' => 'item',              
        '#prefix' => "<ul>",
        '#suffix' => "</ul>",
 foreach ($search_results as $key => $result) {
        $result_html = "<p class=\"matching-title\">" . $result['title'] . "</p>
                <p class=\"matching-desc\">" . $result['description'] . "</p>";

        $form['link-lists']['options'.$key] = array(
            '#type' => 'link',
            '#prefix' => "<li class=\"matching-list-item\">",
            '#suffix' => "</li>",
            '#title' => $result_html,
            '#href' => '#',
            '#options' => array(
              'html' => true,
            '#ajax' => array(
              'wrapper' => 'search-list',
              //'method' => 'html',
              'callback' => 'select_ajax_callback',

  $commands = array();
  $commands[] = ajax_command_replace('#descriptions-list', drupal_render($form['link-lists']));
  $page = array('#type' => 'ajax', '#commands' => $commands);
  return $page; 


I think, the second AJAX call back select_ajax_callback given for the form item link is not correct. It would be thankful if anyone can help me on below questions?

  1. How to set callback for a form item link? Please correct me.
  2. How I will get $form and $form_state values in the function select_ajax_callback. And in this function I need to return another form element which is already defined in the form alter.

Thank You.

Tags : 7 forms ajax

Related Questions

Access Denied when Menu CallBack Function is Called

Updated April 20, 2015 06:03 AM