We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34459
    • 134 Posts
    Hi I have a strange error. I am trying to call [[getResourceField?&id=`2` &field=`1`&processTV=`0` ]] but fill id and tv name with jquery variables. The script bellow, works to some point. I can get id and field value but when I add final "]" to the call via jquery it just want work (last call). Any ideas?

      $(function(){
                // turn the element to select2 select style
                $('#search,#search2').select2();
            });
    
            $(document).ready(function(){
                var str = "";
                var str2 = "";
    	    var groupNr = "";
    
                $(".searchBack").click(function(){
                    $('.searchContainer').removeClass("hidden");
                    $('.searchBack').addClass("hidden");
                    $('.result').addClass('hidden');
                });
                $( "#search" )
                        .change(function() {
    
                            $( "#search option:selected" ).each(function() {
                                str += $( this ).val() + " ";
                            });
                            if($('#search').val()){
                                $('#searchBox2').removeClass('hidden');
                            } 
                        })
    
                $( "#search2" )
                        .change(function() {
                            var group1 = ['PL'];
                            var group2 = ['AD','RS','CH'];
                            var group3 = ['AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','GR','ES','NL','IR','IS','LT','LU','LV','MT','DE','NO','PT','RO','SK','SI','SE','HU','GB','IT'];
                            var group4 = ['SM','MC','LI'];
                            var group5 = ['QQ'];
    
                            $("#search2 option:selected").each(function () {
                                str2 += $(this).val();
                                if ($.inArray(str2, group1) !== -1) {
                                    groupNr ="1";
                                }
                                else if ($.inArray(str2, group2) !== -1) {
                                    groupNr = "1";
                                }
                                else if ($.inArray(str2, group3) !== -1){
                                      groupNr = "1";
                                }
                                else if ($.inArray(str2, group4) !== -1){
                                      groupNr = "44";
                                }
                                else if ($.inArray(str2, group5) !== -1){
                                     groupNr = "33";
                                }
    
                                });
                                if($('#search2').val()){
                                    $('#submit').removeClass('hidden');
                                }
    
    					
                        })
                        .trigger( "change" );
      				$("#submit").click(function(){
                    $('.searchContainer').addClass("hidden");
                    $('.searchBack').removeClass("hidden");
                    $('#search2').hide();
                    $('.result').removeClass('hidden');
    
    
    //Here is the problem- As you can see I've missed the last "]". If I add it to complete the call it all breaks down.
     
    		$('.result').text('[[getResourceField?' + '&id=`'+ $.trim(str) + '`'+' &field=`'+ $.trim(groupNr)+'`' + '&processTV=`1` ]');
    
                });
    
    
    
    
            });
    
      Check out blackflow.pl
      • 3749
      • 24,544 Posts
      I think MODX is seeing the text as a snippet tag and trying to execute it prematurely. You might try this:

       $('.result').text('['+ '[getResourceField?' + '&id=`'+ $.trim(str) + '`'+' &field=`'+ $.trim(groupNr)+'`' + '&processTV=`1` + ' ]' + ']');
      


      A better approach might be to put the text on the page to begin with and use JQuery to replace the values.

      [[!getResourceField? &id=`{{id}}` &field = `{{field}}`  &processTV=`{{processTV}}` ]]            


      I question whether any of this will work as you expect, since if I'm understanding what you're doing, the getResourceField tag will be placed on the page *after* MODX has parsed it and the tag will never be executed.


        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 34459
        • 134 Posts
        I can’t put it live so I'll try to explain.
        User has two dropdowns. Based on the values from them I want to show a specific tv from a specific resource.
        I've wrote it in jquery so I wouldn't have to reload the page.
        Is there any possibility to actually execute this tag dynamically?
          Check out blackflow.pl
          • 4172
          • 5,888 Posts
          you will need an ajax - request to another Resource, where you have placed the getResourceField - snippet.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 34459
            • 134 Posts
            Quote from: Bruno17 at Aug 04, 2015, 04:50 AM
            you will need an ajax - request to another Resource, where you have placed the getResourceField - snippet.

            Ok, so I have to place getResourceField request in a resource and place the snippet with my script. But how then I can pass variables to the getResourcesField?
              Check out blackflow.pl
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              If I understand correctly what you want to do, it might work like this:

              On the current page (the one the visitor is looking at) you have a bit of Javascript that makes an AJAX request, which may also include GET or POST values, to a resource that will return the data that the Javascript will insert into the page.

              In your specific case, on selecting a dropdown option, the AJAX call will get triggered, passing the value of the dropdown option the user selected.

              That AJAX resource will receive any GET or POST values the AJAX request includes. It needs to have a snippet to retrieve those values, validate and sanitize them, then either use them itself to generate the desired output, use them in a runSnippet() function, or put them into placeholders for some other snippet on the same resource to use.

              That AJAX resource will not need a template, since it's just returning a portion of data that the receiving Javascript will process and insert.

              The only reason you use another resource with snippets for this is to be able to conveniently make use of the full MODX API. You could use a separate .php file altogether, loading the modx object in API mode, if you are more comfortable working that way.
              [ed. note: sottwell last edited this post 11 years, 2 months ago.]
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 34459
                • 134 Posts
                ok, I think I've done it! Thx for your input. I'll try to post the whole code, maybe somebody could use it. Still, any ideas of improving it are more then welcome
                So this is my ajax call on my main page, after jquery stuff.
                I've created an additional variable dynamicTv- which contains values from my dropdowns.
                $.ajax({
                  type: "POST",
                  url:"[[++site_url]][[~2]]",
                  data: {dynamicTv:dynamicTv},
                  success: function(data){ 
                            $(".result").html(data); 
                         },
                
                });
                


                Then as suggested, I've created an empty resource with this snippet

                $dynamicTv = $_POST['dynamicTv'];
                $page = $modx->getObject('modResource', 1);
                return $page->getTVValue($dynamicTv);
                

                  Check out blackflow.pl
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  That's how to do it. One way, anyway. If it works, then it's the right way wink
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 4172
                    • 5,888 Posts
                    Looking good to me.

                    If you want to restrict to allowed TVs only, you could do it like that:

                    $allowed_tvs = 'tv1,tv2,tv3,tv4';
                    
                    $allowed_tvs=explode(',',$allowed_tvs);
                    $dynamicTv = $_POST['dynamicTv'];
                    
                    if (in_array($dynamicTv,$allowed_tvs)){
                        $page = $modx->getObject('modResource', 1);
                        return $page->getTVValue($dynamicTv);
                    }
                    
                    return 'you are not allowed to view the value of this TV';

                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Definitely you should do something about validating and sanitizing that POST value before you use it for anything, especially using it in a database query.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org