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

    Im having trouble passing variables to a snippet using href.

    I want to have some images act as buttons, and when they are clicked, they will pass some variables to a snippet which should be run.

    The images and href is placed in a chunk and the code is like:
    <a href=[[Rater? &input=`true`]]><img src="assets/something.png"></a>
    


    I've also tried:
    <a href="[[Rater? &input=`true`]]"><img src="assets/something.png"></a>
    


    But none of this will work properly. The last one can run the snippet, but I cant get the value from the variable $input in the snippet.

    The snippet should perform an if statement like:
    If ($input == "true") {
    //some code }
    else {
    //something else
    }
    


    Can anyone help me with the correct syntax, or else, a better way to run a snippet and send variables from an image that is clicked?

    Thanks a lot in advance

    This question has been answered by multiple community members. See the first response.

      • 34193
      • 330 Posts
      are you sure that the second one runs the snippet, because I don't think you can do what you are trying. Since modx would run the snippet on page load and swap out the snippet call for what ever was returned by the snippet. eg

      <a href="[[Rater? &input=`true`]]"><img src="assets/something.png"></a>

      Becomes
      <a href="hello world"><img src="assets/something.png"></a>

      on page load if your snippet was

      If ($input == "true") {
         return "hello world" }
      else {
         return "good bye"
      }


      the only way I can think of doing this is to have a blank page with the snippet call in it and link to that page in the href eg.

      <a href="process.html?input='true' "><img src="assets/something.png"></a>


      and then use the standard $_GET['input'] inside the snippet.
      • discuss.answer
        • 39357
        • 13 Posts
        Thanks for the reply.

        Yes, im sure the code runs the snippet, because the if statement sends information to a DB.
        The value "true" if the "if" statement is true, and the value "false" if not.
        The "else" condition sends the value "false" to the snippet is executed, but the value passes from the chunk is not read sad.

        I've now tried
        $passedvariable =  $_GET['input']
        


        But that doesnt return any value either.
        How can I eventually debug the code to see what value the variable "input" has when the snippet is executed?
        • discuss.answer
          • 32316
          • 387 Posts
          Pete is correct - snippets (if called uncached) are run and then the page is 'served up to the browser'

          If you want to run a snippet you need to load a page or part of a page (AJAX)... So:

          You could have the image links calling different resources (normal or AJAX request), which in turn would cause a snippet to be run, the different pages (modx resources) could pass different values to a common snippet.

          Or you could call one resource (this could be the same page that has the links) with different 'GET' parameters.
          <a href="my page?input="1"><img src="assets/something.png"></a>
          <a href="my page?input="0"><img src="assets/somethingelse.png"></a>
          

          and in the snippet:
          if ($_GET['input'])
          {
          do something
          }

          Of course you should NOT just use GET without sanitizing - unless you like living dangerously, I'm sure there is a modx function to use the GET parameters nicely - I don't remember off the top of my head and searching the docs is something I do not have enough time or energy for now. This forum post might be useful https://forums.modx.com/index.php?topic=36096.0.
          This example would in fact be easy to sanitize since you would only be expecting input = 1 or 0 so only except those two inputs.

          Or you could use a form with the images as buttons, and pass the parameters as POST data - don't forget to sanitize!

          • discuss.answer
            • 39357
            • 13 Posts
            Thanks a lot for your help - now I've got it working smiley
            Just by moving the snippet to be run at the start of the page and using the link:
            href="?Variable=1 - and some other variables
            


            Then starting the snippet with an If statement, if certain variable = true, then run snippet, else not.
            The link refers to the starting page so the code loops with the new variables.

            Its used for a voting system where the user votes on images where the vote is passed to the snippet and assigned to rows in a DB.

            But again - thanks a lot smiley