How to add AJAX call back from another AJAX result in a form 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 ALTER

  $form['searchbutton'] = array(
      '#type' =&gt; 'button',
      '#value'=&gt; t('Search'),
      '#id'   =&gt; 'search_button',
      '#ajax' =&gt;  array(
          'callback' =&gt; 'ajax_search_callback',
          'wrapper' =&gt; 'search-list-wrapper',
          'method' =&gt; 'replace',
          'effect' =&gt; 'fade',
          'progress' =&gt; array('type' =&gt; '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' =&gt; 'item',              
        '#prefix' =&gt; "&lt;ul&gt;",
        '#suffix' =&gt; "&lt;/ul&gt;",
    );
 foreach ($search_results as $key =&gt; $result) {
        $result_html = "&lt;p class=\"matching-title\"&gt;" . $result['title'] . "&lt;/p&gt;
                &lt;p class=\"matching-desc\"&gt;" . $result['description'] . "&lt;/p&gt;";

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

 }
  $commands = array();
  $commands[] = ajax_command_replace('#descriptions-list', drupal_render($form['link-lists']));
  $page = array('#type' =&gt; 'ajax', '#commands' =&gt; $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?


How to set callback for a form item link? Please correct me.
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.
You are adding form elements in the ajax callback. These will never work, as the form has already been cached before the ajax callback is reached. You need to add your form elements in the original form definition, or a _form_alter hook. Use values in the form state to determine whether or not the form has already been submitted.