We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36571
    • 145 Posts
    Hey all,

    There may be a super-simple solution for this, but I can’t seem to find a way. I want to pass a variable in my URL to the ditto chunk that is output on the page.

    The URL is http://www.mysite.com/page.html?somevar=somevalue

    And the ditto chunk is

    <div><a href="[+url+]">[+pagetitle+]</a><br />[+introtext+]</div>


    I would like the link in the chunk to go to
    [+url+]?somevar=somevalue
    but obviously I can’t put a snippet like
    <?php $_REQUEST['somevar']; ?>
    in the chunk, which would be the easiest solution.

    Anyone got any ideas?

    Thanks
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Use a TV with
      @EVAL return $_REQUEST['someval']; 

      then you can use [+tvname+] in your tpl (I think; not tested).

      I tend to avoid using URL strings; to pass a value I can use a mini-form with a submit button styled to look like a link or whatever I want, or if there are several options use a fancy radio button block, and pass the value in the POST instead.
        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
        • 7455
        • 2,204 Posts
        you can use a small snippet for that, just put the code in a snippet and call it in t he ditto template. (make sure you striptags etc for security)

        snippet contents would be something like this:
        <?php 
        $output =  strip_tags($_GET['somevar']); 
        if isset($_GET['somevar']){
           return "?somevar=".$output;
        }else{
        return;
        }
        ?>
        


        then the chunk would be like this:
        <div><a href="[+url+][[snippet]]">[+pagetitle+]</a><br />[+introtext+]</div>
        



        make sure that you call you ditto uncached [!ditto....!]

        Dimmy

          follow me on twitter: @dimmy01
          • 36571
          • 145 Posts
          Great. Thanks Dimmy.

          You had a minor typo, so I tweaked the code a tiny bit

          <?php
          $output =  strip_tags($_GET['postcode']); 
          if (isset($_GET['postcode'])){
             return $output;
          }else{
          return;
          }
          ?>


          Works perfectly.

          I knew there was an easy fix. Thanks.