We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49529
    • 196 Posts
    comp_nerd26, MODX definitely do not restrict any array manipulations, any correct PHP code should work well in snippets. Error records in log apparently related to pdoTools extra, so try to reinstall it. When you tell that your PHP code runs well outside MODX, do you mean your local dev server or "public" hosting? You may check mod_security settings and PHP (not MODX) error log.
      • 47401
      • 295 Posts
      hi

      thanks for all your responses. ive managed to get the snippet to work, but the only problem is that the redirect isnt working, although my custom snippet returns true as the last line in the snippet. not too sure why it doesnt re direct. my formit code is?

      [[!FormIt?
      &hooks=`hook.RampartFormit,spam,email,FormSave,redirect,sendToZoho`
      &emailTpl=`send-foot-email`
      &emailTo=`[email protected]`
      &redirectTo=`8`
      &submitVar=`sendfooter`
      &validate=`fullname:required,
      Email:email:required,
      nospam:blank,
      Phone:required`
      ]]

      the hook sendToZoho has the following php code in:


      <?php
      $valid = array();
      $modx = new modX();
      $arr = $modx->sanitize($_POST);
      array_pop($arr);
      $newarray = array();
      $newarray[] = '<Leads><row no="1"><FL val="Lead Source">Web</FL>';
      foreach ($arr as $k=>$v) {
          if($k == 'Email') {
              // checks email 
              $email = $v;
              $regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^"; 
              if (preg_match($regex, $email)) 
                      { 
                          $valid['Email'] = true;
                          
                      } else  { 
                          $valid['Email'] = false; 
                          
                      } 
          }
          
          if($k == 'LEADCF13') {
              // checks message
              if(preg_match('/^[a-zA-Z0-9 .\-]+$/i', $v)) {
                  $valid['message'] = true; 
              } else {
                  $valid['message'] = false;
              }
          }
          
          if($k == 'Phone') {
              $phone = $v;
              $input = str_replace(' ', '', $phone);
              
              if($input && strlen($input) > 9) {
              $pattern = "^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{5}\)?[\s-]?\d{4,5}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$^";
              
              $match = preg_match($pattern,$input);
              
                  if ($match != false) {
                      // We have a valid phone number
                      $v = $input;
                      $valid['Phone'] = true;
                  } else {
                      // We have an invalid phone number
                      $valid['Phone'] = false;
                  }
              
              } else {
                  $valid['Phone'] = false;
              }
              
          }
          
          
          if($k == 'fullname') {
              $names = explode(' ', $v);
              if(isset($names[0])) {
              $newarray[] = "<FL val='First Name'>{$names[0]}</FL>";
              }
              if(isset($names[1])) {
              $newarray[] = "<FL val='Last Name'>{$names[1]}</FL>";
              }
          continue;
          }
          $newarray[] = "<FL val='$k'>$v</FL>";
      }
      $newarray[] = '</row></Leads>'; 
      $data = str_replace(',','',implode(',', $newarray));
      
      if(!in_array(false, $valid)) {
      try {
       //Initialize connection 
        $ch = curl_init('https://crm.zoho.com/crm/private/xml/Leads/insertRecords?');
        curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// Turn off the server and peer verification 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response) 
        curl_setopt($ch, CURLOPT_POST, 1);//Regular post 
        //Set post fields 
        //this script is being proccessed by a form so I also put all of my $_POST['name'] variable here to be 
        //used in the $xmlData variable below
      
        $authtoken = "THIS_IS_MY_TOKEN_RECEIVED_FROM_THIRD_PARTY";
      
        $query = "newFormat=1&authtoken={$authtoken}&scope=crmapi&xmlData={$data}"; 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl. 
        //Execute cUrl session 
        $result = curl_exec($ch);
            throw new Exception('zoho crm export : '.$result);
        
        curl_close($ch);
      } catch(Exception $e) {
          $modx->log(xPDO::LOG_LEVEL_ERROR,$e);
      }
      }
      
      return true;






      ok, so the email gets generated, the FormSave saves the data to the modx database correctly, and the xml is sent to the zoho crm system. everything works fine, except for the blank screen and no redirect.






      cheers
        • 49529
        • 196 Posts
        For some reason i was sure that redirect should always be the last hook, otherwise all hooks after it will not run. Am i wrong?
        Blank screen usually tells us that there are some PHP errors. Try to enable PHP error reporting on your page:
        error_reporting(E_ALL); 
        ini_set("display_errors", 1);
        

        , and see what will be shown. [ed. note: whitebyte last edited this post 11 years, 8 months ago.]
          • 47401
          • 295 Posts
          ok thanks whitebyte, did this thanks. the white screen shows php error:

          Fatal error: Call to a member function setProcessingElement() on a non-object in /var/www/domain.co.uk/public_html/core/model/modx/modscript.class.php on line 98

          not too sure what this error means
            • 47401
            • 295 Posts
            i have also tried indervidually removing each hook and executing the form after removing each hook, which it seems that the error message only reports when my custom snippet is called.
              • 49529
              • 196 Posts
              Brief look into mentioned file tell us that there are probably some problems with parsing MODX tags (something in between "[[" and "]]".
              Please double-check your template syntax, try to temporary comment/remove most of MODX tags to see what exactly goes wrong.
                • 47401
                • 295 Posts
                hi

                im not so sure that is the problem. if that was the case wouldnt the template that shows the formit code show a white screen prior to submission and would not show anything? when I remove my custom snippet from the formit command and submit the form, the form seems to work fine with no errors, sends an email, submits to FormSave and redirects. could it be because im instantiating the modx class as variable $modx?
                • discuss.answer
                  • 49529
                  • 196 Posts
                  Honestly i dont'know. BTW's the reason to make new instance of modX inside snippet? $modx is always available inside snippet as a global, so you don't need to do this
                  $modx = new modX();


                  I suppose this line may be safely removed.
                    • 47401
                    • 295 Posts
                    HI, ok thats great, i dont get the error message now, and it seems to be redirecting fine. the only problem is that its not retreiving the post data. is that still the correct way to assign post array to a php variable, or does modx have a function for this?
                      • 47401
                      • 295 Posts
                      its all working now. thats great thank you for your help