We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47401
    • 295 Posts
    I think i must be doing something wrong. i have a snippet which is trying to loop through the form data that is being submitted. using php

    $data = '';
    foreach($_POST as $key->$value) {

    $data .= $key." ".$value;

    }

    do I need to pass the data to the snippet 1st, or can I call the $_POST from within the snippet?
    i guess if i need to pass the data 1st do I need to call the snippet from a chunk?


    im basically trying to convert the $_POST array to a string. I could of course use $string = explode(',', $_POST); but i need to format each input box 1st ad prepaire the data for 3rd party crm system.

    This question has been answered by whitebyte. See the first response.

      • 49529
      • 196 Posts
      $_POST is accessible inside snippet. Your code looks OK, i think you can just drop it into a snippet ant then call it in the template or chunk.
        • 47401
        • 295 Posts
        hi

        thanks your your response. every time i click the form it produces a white screen. possible typo not too sure still looking into it.
        i have found another, yet safer way:

        $modx = new modX();
        $sanitizedRequests = $modx->sanitize($_POST);

        then loop the variable (array) $sanitizedRequests
          • 47401
          • 295 Posts
          i dont need the snippet to output anything. all im trying to do is sanatize the data and prep it for sending to crm system. just need to return true if sucessfull or false un sucessfull.
            • 47401
            • 295 Posts
            looks like modx doesnt like me to loop through arrays
              • 47401
              • 295 Posts
              this is the code i have at the moment in 1 snippet

              $modx = new modX();
              $arr = $modx->sanitize($_POST);
              array_pop($arr);
              echo "<pre>";
              print_r($arr);
              echo "</pre>";
              $newarray = array();
              foreach($arr AS $k->$v) {
              $newarray[] = "<FL val='$k'>$v</FL>";
              echo $k;
              exit;
              }

              $data = implode(',',$newarray);

              var_dump($newarray);
              exit;

              this works fine outside a modx environment, but for some strange reason modx doesnt like data manipulation i.e creating a new array from an original array. the $arr prints the array out fine, yet the $newarray is none existant when var_dumping it out to the screen. very strange
                • 47401
                • 295 Posts
                and $data= NULL
                  • 47401
                  • 295 Posts
                  tried different code, but still no joy. does modx restrict the use of php commands?

                  outside modx this works fine, in a snippet when executing just shows a white screen, nothing outputted to the screen.

                  $modx = new modX();
                  $arr = $modx->sanitize($_POST);
                  $newarray[0] = '<Leads><row no="1"><FL val="Lead Source">Web</FL>';
                  array_pop($arr);
                  foreach($arr AS $k->$v) {
                  $ne = "<FL val='$k'>$v</FL>";
                  array_push($newarray, $ne);
                  }

                  $data = implode(',',$newarray);

                  var_dump($data);
                  exit;
                    • 47401
                    • 295 Posts
                    im wondering if this system error has anything to do with it. seem to get this error when i refresh the page.

                    [2015-02-02 16:20:00] (ERROR @ /index.php) Could not load class: pdofetch from pdotools.pdofetch.
                    [2015-02-02 16:20:00] (ERROR @ /var/www/pensiontransferexperts.co.uk/public_html/core/cache/includes/elements/modsnippet/59.include.cache.php : 15) PHP warning: Creating default object from empty value
                      • 4172
                      • 5,888 Posts
                      what about this in a snippet:

                      <?php
                      
                      $arr = $modx->sanitize($_POST);
                      array_pop($arr);
                      echo "<pre>";
                      print_r($arr);
                      echo "</pre>";
                      $newarray = array();
                      foreach ($arr as $k => $v) {
                          $newarray[] = "<FL val='$k'>$v</FL>";
                      }
                      
                      $data = implode(',', $newarray);
                      
                      var_dump($newarray);
                      
                      
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!