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

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

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

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.