We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Ajax call to custom module

Development and Programming

James Chambers's avatar
James Chambers
7 posts
14 years ago
James Chambers's avatar James Chambers

Hi there!

I’m making a simple ajax call using jquery to a custom module. The jQuery is like so:

var text        = 'text';
        var orderby        = 'order';
        var source        = 'source';
        var media        = 'media';
        var tags        = 'tags';
                        
        var fetchUrl     = '/search/results/ ' + text + '/' + orderby + '/' + source + '/' + media + '/' + format + '/' + tags;
        
        
        $.ajax({
                type: "POST",
                url: fetchUrl,
                cache: false,
                data: "after=000000",
                success: function(data){ 
            
                    searchResults.html(data);

                    $('#results').masonry({
                      columnWidth: 360, 
                      itemSelector: '.related' 
                    });
                }
            });

That’s calling the template search/results.html:

{exp:aq_resources:search text="{segment_3}" orderby="{segment_4}" source="{segment_5}" media="{segment_6}" format="{segment_7}" tags="{segment_8}"}

Then the search function in aq_resources (the TMPL library is being loaded in the parent class):

function search()
    {
        $output                = '';
        
        $text                = $this->EE->TMPL->fetch_param('text');
        $orderby            = $this->EE->TMPL->fetch_param('orderby');
        $source                = $this->EE->TMPL->fetch_param('source');
        $media                = $this->EE->TMPL->fetch_param('media');
        $format                = $this->EE->TMPL->fetch_param('format');
        $tags                = $this->EE->TMPL->fetch_param('tags');
        
$output .= $text . $orderby;

        return $output;
    }

All of the TMPL->fetch_params are returning blank values. Anyone got any ideas as to why it might not be working?

Thanks!

James

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Hey James,

A few things I’d try:

  • Access your URL (/search/results/text/orderby/source/media/format/tags) directly to see if you get the same result
  • Before hitting the URL with an ajax request, verify the URL is correct in your jQuery by logging it to the console or pop up an alert
  • Confirm your plugin/mode is getting the values correctly by using the Template Logger to debug (more info here)
       
James Chambers's avatar
James Chambers
7 posts
14 years ago
James Chambers's avatar James Chambers

Hi Erik,

Thanks very much for getting back to me. I tried hitting /search/results/text/orderby/source/media/format/tags directly as suggested and am still getting a blank result, so it looks as though there’s a problem with getting values through the fetch_param() function to the plugin.

I’ve also tried creating a new reference to the EE_Tmpl library like so:

$TMPL                 = new EE_Template;
        $text                = $TMPL->fetch_param('text');
        $orderby            = $TMPL->fetch_param('orderby');
// etc....

With no success. Can you think of any reason why the values might not be getting passed through fetch_param()?

Thanks again!

James

P.s. - Thanks for the info on the Template Logger, very useful!

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
Can you think of any reason why the values might not be getting passed through fetch_param()?

My first guess is that the values getting passed are empty strings. Try this:

• Create a new template that only contains your plugin tag • Add your tag in to include just one of those parameters (try ‘text’ for example) • Update your search() method to start with the following code:

<?php

public function search()
{
     exit(var_dump($this->EE->TMPL->fetch_param('text')));

Then load your template and see what spits out. If that shows your expected value of ‘text’ then it’s probably an issue with the way your jQuery request is working with your template.

       
James Chambers's avatar
James Chambers
7 posts
14 years ago
James Chambers's avatar James Chambers

It looks as though there was something in the parent class which was affecting the template values getting through (there was a fair bit of cruft in there which must have been causing problems). I’ve now made a new plugin which is returning the values fine, however they aren’t coming back through the ajax request which means something’s amis with my jQuery.

Any ideas gratefully received, otherwise will push on and post back with updates.

       
James Chambers's avatar
James Chambers
7 posts
14 years ago
James Chambers's avatar James Chambers

Ok, so I’m able to get the proper values returned when I hit /search/results/text/orderby/source/media/format/tags via ajax with static text in the tags. For example:

{exp:aq_search text="test string" orderby="test string" source="test string" media="test string" format="test string" tags="test string"}

returns the test strings just fine. However when I try:

{exp:aq_search text="{segment_3}" orderby="{segment_4}" source="{segment_5}" media="{segment_6}" format="{segment_7}" tags="{segment_8}"}

None of the strings are passed through to the plugin. So long story short the segment vars seem to be being populated properly when I visit the page directly, but not at all when I hit it with ajax.

Does anyone have any ideas about why this could be happening? I’m thinking something to do with parse order?

Thanks

       
James Chambers's avatar
James Chambers
7 posts
14 years ago
James Chambers's avatar James Chambers

Solved! For anyone else who comes across a similar thing the issue was that I was loading the template class before the fetch_param()s. Moving:

$this->EE->load->library('Template', NULL, 'TMPL');

After the fetch_param() functions sorted me out.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

I didn’t realize you were loading the library like that. Within a plugin you don’t need to load the library. That may have been causing the issue.

Regardless, glad it’s working 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.