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

    how do you pass $_POST and $_GET variables to snippets (or chunks)? Is there a standard way to do this? Do I have to develop a special snippet for this myself?

    regards,
    JB
    • Quote from: jbjoe at Mar 05, 2010, 07:54 PM

      how do you pass $_POST and $_GET variables to snippets (or chunks)? Is there a standard way to do this? Do I have to develop a special snippet for this myself?
      All snippets have access to GPC variables as they would in any PHP script. You don’t have to "pass" anything. As for chunks, in Revolution you can pass properties to a chunks, but again, you should use a snippet to access any GPC variables:
      <?php
      /* process a chunk with the $_REQUEST vars as placeholders */
      $output = $modx->getChunk('myChunk', $_REQUEST);
      ?>

      though something like this is also possible:
      [[!$myChunk? &myPostVar=`[[!getValue? &var=`_GET` &key=`myPostVar`]]`]]

      where getValue is a snippet like such:
      <?php
      /* 
      getValue: an example snippet to get a scalar variable value 
          or optionally a scalar array entry (specify key)
      
      PROPERTIES:
      &var - the name of the scalar variable or array to get a value from
      &key - the index key of an array variable specified in &var; required if &var is an array 
      
      */
      $value = '';
      if (!empty($var) && isset($$var)) {
          if (is_scalar($$var)) {
              $value = (string) $$var;
          }
          if (is_array($$var) && !empty($key) && array_key_exists($key, $$var)) {
              if (is_scalar($$var[$key])) {
                  $value = (string) $$var[$key];
              }
          }
      }
      return $value;
      ?>
        • 13735
        • 62 Posts
        Hello,

        thx that helps a lot! What I don’t understand is the [tt]$$var[/tt] in your code. Never saw that before..

        regards,
        JB
        • Quote from: jbjoe at Mar 05, 2010, 10:01 PM

          thx that helps a lot! What I don’t understand is the [tt]$$var[/tt] in your code. Never saw that before..
          This is known as a variable variable. Redundant, I know, but since the value of the var property in my example was _GET, and in our Snippet &var is extract()’d to be $var, $$var is equivalent to $_GET. See http://php.net/manual/en/language.variables.variable.php for more information.
            • 13735
            • 62 Posts
            Ah yeah that makes sense. Thx.
              • 13735
              • 62 Posts
              Well...

              Please note that variable variables cannot be used with PHP’s Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically...

              I will take that now definitly into account. laugh So if you use the code above, then alter it to fit your purposes.

              JB