0

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' => '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.

1 Answer 1

0

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.

1
  • Thanks for your response. I will try and will update the result here.
    – Aswini K
    Jul 18, 2017 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.