Using ‘return’ with an ajax callback renders an entire page before my content is loaded

I’m developing a module that uses Openlayers maps and Drupal Commerce. When you click on the map, you should see products that relate to those coordinates of where you click. I have this working perfectly.

I’ve got a ‘foreach’ inside of my function that creates each product row:

foreach($alldata as $data) {     $content = '';     $date = substr($data['_source']['ver_date'], -4);         $content .= '<div class="product-row" data-date="' . $date . '" data-id="' . $id . '" data-scale="' . $data['_source']['scale'] . '">';      $titleString = '';     $titleString .= $data['_source']['longname'] . ' ';     $trimmedTitle = strlen($titleString) > 80 ? substr($titleString,0,80)."..." : $titleString;     $content .= '<span class="title">' . $trimmedTitle . '</span>';      $content .= '</div>';     print $content; } 

This works exactly like it should and renders the rows of products exactly like I want.

The problem I am having is that there is an ‘Add to Cart’ button that was created within another function somewhere else in the site that I have to render in this foreach.

$arr = explode(".", $data['_source']['price']); $inner_html = '<span class="btn-text">$'.$arr[0].' Print</span><span class="glyphicon glyphicon-shopping-cart"></span>'; $add_to_cart_btn = drupal_get_form('myModule_product_cart_button', $inner_html, (object)$data['_source']); $content .= '<div class="product-cart">' . drupal_render($add_to_cart_btn) . '</div>'; 

The button loses it’s Ajax properties when I use ‘print $content’. So I changed it to ‘return $content’ and moved it outside the ‘foreach’.

I get all of the content properly as well as a working ajax ‘Add to Cart’ button. However, I also get the entire page template rendered prior to all of my content that I’m returning.

<--- Content Rendered In Div via Ajax --->  Full Empty Page Template --------------------------- Ajax content formatted properly and working properly 

I’ve tried using ajax_render. I’ve tried using ajax_command_html. I’ve tried completely redoing my function and ajax to use drupal.behaviors. Nothing is working to remove that full page from rendering prior to my content. It only happens I when use ‘return’. I’ve also tried adding drupal_page_footer().

Even when I have absolutely nothing in my foreach, if I use ‘return $content’, I get a full page rendered. And it’s not the use of ‘$content’. I’ve changed that to other variable namespaces and it still renders an empty page template.

Here is my ajax call:

 $('.products').mousedown(function() {     $.ajax({       url: Drupal.settings.myModule.ajaxUrl,        method: 'post',       data: {          longtitle : title,         id : id        },        dataType: "text",                 success: function(data) {         var markup = '';         markup += data;            $('.products-pane').html(markup);        }     });      });  

Anyone know what is causing this?

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

Using ‘return’ with an ajax callback renders an entire page before my content is loaded

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.