ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Need to feed dynamic variables via querystring to a template holding an {exp:query}

April 08, 2011 7:02pm

Subscribe [4]
  • #1 / Apr 08, 2011 7:02pm

    a la mode

    168 posts

    Ok guys/gals…

    This is like a custom search.

    I need help with this one. I have a form that resides in a modal style dialog box (jQuery). When the user changes the select options, I need to trigger the jQuery to push the values to the template and return the results in a div. I can do it with hard coded values, but not with variables in the query or the .load.

    Without trying to bore people with code, it is a standard select(ddl) with options.
    When the user makes a selection, I am trying to catch it like this:

    $("#select1, #select2, #select3, #select4").change(function(){
        // Check for search data
        var variable_a = $("#select1").val().toString();
        var variable_b = $("#select2").val().toString();
        var variable_c = $("#select3").val().toString();
        var variable_d = $("#select4").val().toString();
                    
        // Build out query string to pass
        var qs = "?";
            qs += "variable_a="+variable_a+"&variable_b="+variable_b+"&variable_c="+variable_c+"&variable_d="+variable_d;
        
        // Build the link to pass
        var url = "/TemplateGroup/template"+qs;
                    
        // Call method to populate users selection
        loadResults(url);                    
    });

     

    The loadResults(url) function is here:

    function loadResults(url){
        $("#myDiv").load(url, function(){
            alert("Search Completed");
        });
    }

     

    I need the template to grab the querystring results and pass them into the query to run and show the results.

    The template looks like this:

    {exp:query limit="5" sql="SELECT DISTINCT ce.title, m.screen_name, cd.field_id_58 FROM (exp_channel_titles ce, exp_members m, exp_channel_data cd) WHERE ce.site_id = '6' AND ce.channel_id IN (18) AND ce.author_id = m.member_id AND ce.entry_id = cd.entry_id ORDER BY ce.entry_date DESC"}
        {title} - {screen_name} - {field_id_58}
            {paginate}
            Page {current_page} of {total_pages} pages {pagination_links}
        {/paginate}
    {/exp:query}

    I figured this would be easier than it is turning out to be. Either that, or my head is just looking at the wrong stuff…

    I hope this all makes sense. Thanks a bunch in advance.

  • #2 / Apr 09, 2011 12:23am

    Rick Jolly

    729 posts

    You could wrap your template code in plugin tags. In your custom plugin code, you could get the data from the query string. Do you have $_GET working with EE? If not, you can use an ajax post.

  • #3 / Apr 11, 2011 2:16pm

    a la mode

    168 posts

    Rick,

    I haven’t been able to get $_GET working in EE. Is there a trick for that? It would help tremendously.

    Ha… and I forgot about the POST options, so I changed my load function to be:

    $("#myDiv").load("/Template_Group/template", "{'var1':var1,'var2':var2,'var3':var3,'var4':var4}", function(){
        alert("Search Completed");
    });

    But that didn’t work either.

    I may not be grabbing the POST data correctly though.

  • #4 / Apr 11, 2011 2:24pm

    Rick Jolly

    729 posts

    The post object shouldn’t be in quotes. Also, quotes around post variable names are optional except if using a javascript key word.

    $("#myDiv").load("/Template_Group/template", {'var1':var1,'var2':var2,'var3':var3,'var4':var4}, function(){
        alert("Search Completed");
    });
  • #5 / Apr 11, 2011 2:38pm

    a la mode

    168 posts

    When I remove the double quotes, I get :

    Uncaught SyntaxError: Unexpected token ,

    ... and it renders in the page like this:

    $("#search_results").load("/Query_Group/all_emails", , function(){
        alert("Search Completed");
    });
  • #6 / Apr 11, 2011 2:54pm

    Rick Jolly

    729 posts

    Ok, I’m not familiar with the jquery load() function. I always use the ajax function. This example is from the query docs, which you’ve probably read:

    $("#feeds").load("feeds.php", {limit: 25}, function(){
    alert("The last 25 entries in the feed have been loaded");
    });
  • #7 / Apr 11, 2011 3:02pm

    a la mode

    168 posts

    Right… That’s how I had it built. The problem seems to be that since it uses ‘{' and '}’, EE tries to read it and it isn’t being delivered when the page is rendered. That’s the reason why I tried wrapping it in double quotes.

    What ajax function do you use? (Essentially, that’s all this is too) Maybe I can achieve what I am after using a different method.  😉

  • #8 / Apr 11, 2011 3:10pm

    a la mode

    168 posts

    I used :

    $.ajax({
        type: "POST",
        url: "/Template_Group/template",
        data: "v1="+v1+"&v2;="+v2+"&v3;="+v3+"&v4;="+v4+"",
        success: function(data){
            $("#myDiv").html(data);
            alert("search completed");
        }
    });

    Which gave me the same results as when I wrapped the .load() in double quotes.

    Like I said earlier though, I could be handling the data wrong on the back end…

    Currently, I am doing this in the template:

    <?php
        $v1 = $_POST["v1"];
    ?>
    {exp:query limit="5" sql="SELECT DISTINCT ce.title, m.screen_name, cd.field_id_58 AS Whatever FROM (exp_channel_titles ce, exp_members m, exp_channel_data cd) WHERE ce.site_id = '6' AND ce.channel_id IN (18) AND cd.field_id_59 = '$v1' AND ce.author_id = m.member_id AND ce.entry_id = cd.entry_id ORDER BY ce.entry_date DESC"}
        {title} - {screen_name} - {Whatever}
            {paginate}
            Page {current_page} of {total_pages} pages {pagination_links}
        {/paginate}
    {/exp:query}
  • #9 / Apr 11, 2011 3:13pm

    Rick Jolly

    729 posts

    Ah, EE is parsing the javascript - that makes sense now. Do you have your javascript within script tags? You might need to put the js in a “template type” of “javascript” or “static”.

  • #10 / Apr 11, 2011 3:15pm

    Rick Jolly

    729 posts

    Yes, you have to echo the php variables within the exp:query

    <?=$v1?> or <?php echo $v1; ?>

    Also, you’re going to want to guard against sql injection.

  • #11 / Apr 11, 2011 3:44pm

    a la mode

    168 posts

    Made all of the changes, but now I get a Server 500 error with the POST data like I was with the GET data.

    POST http://sandbox.com/Template_Group/template 500 (Internal Server Error)
    :grrr:

  • #12 / Apr 12, 2011 11:23am

    a la mode

    168 posts

    Rick, Thanks for the help.

    I got this figured out. I was using

    <?=$v1?>
    , but it only liked
    <?php echo $v1; ?>

    .

    I’m not worried about injection for this template set as this is only going to be used in-house as an email campaign tool. Kudos for addressing that though.

    Now I get to build the fun, complex statement.  😊

  • #13 / Apr 12, 2011 12:01pm

    Roi Agneta

    352 posts

    Brian,

    I have done this a number of times using .get.  Here is my code:

    $("input.mybutton").click(function() {
        var cat = $('select#category').val();  //gather user selections
        var src = $('select#source').val();
        var tgt = $('select#target').val();
        var combo = src + '-' + tgt;
        $("#ad_col2box").hide("slow");
        $.get(
            "template_group/template", 
            {certification:cat, languages:combo}, //pass user variables
            function(data){
                $("#listings").html(data);   //put results in "listing" div
    
        });
    
    });

    The template being called contains a custom query that returns member info based on the search criteria - if no results, it returns an error message.

    You can see the actual page here:http://www.atia.ab.ca/index.php/directory.

    Hope the helps!

    Roi

  • #14 / Apr 12, 2011 12:18pm

    a la mode

    168 posts

    Roi,

    Thanks for the info. I ended up with a very similar script. My problem ended up being on the back end when I was running the query though. I think I have a grip on it now, I just have a fun trip in front of me with multiple joins, etc…  :sick:

    It is an email campaign builder that uses EE to hold all of the email data but has variable fields, templates, authors, etc. Just a bunch of custom fields.

    I like your search and contact card design. Clean!  :coolsmile:

  • #15 / Apr 12, 2011 12:22pm

    Roi Agneta

    352 posts

    Brian - glad to hear you got it under control!  Yeah, doing joins on EE tables is a ton of fun…I had to do it on the example given, but it was relatively straightforward in that case.  Just a thought - if you have complex relationships you may want to take a look at Playa and see if that would help.

    Roi

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases