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


    Hello,

    I have the following function in a snippet:
    <?php
    if(!function_exists('my_OnBeforeMailSent')) {
    
    	function my_OnBeforeMailSent(&$fields) {
    
    		global $modx;
    	
    		// *** Save IP
    		$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    		$fields['ip'] = $ip;
    	
    		// *** Get the Get Params
    		$x = trim(urldecode($_REQUEST["x"]));
    	
    		// *** Add data to the fields array
    		$fields['x'] = ($x==""?"UNKNOWN":$x);
    
    		return true;
    		} // End Function
    		
    } // End If Function Exists
    ?>
    

    And in the eForm-Report Chunk I have the following:
    <p>
      IP = [+ip+]<br />
      X = [+x+]
    </p>
    

    The form is called with www.mysite.com/form.html?x=abc
    I would expect that ’abc’ gets send along in the email. But it’s not.
    Anybody having any idea why not?

    Workaround I tried was to fill a hidden value with the Get variable using Javascript. But then I got the eForm error which makes sense because probably the tampering-detection-routine kicks in.

    So, the question is how to pass on values from other documents to the eForm-Form and let them be mailed to the recipient.

    Thanks for your help.

    Regards,

    Bert
      • 3177
      • 137 Posts
      Since no reaction on my first question, let me rephrase it in simpler format:

      I want to pass variables from a document to the eForm form and mail them to the recipient.
      How do I do that?

      My guess is by $_GET params which are picked up by OnBeforeMailSent, but this does not work.

      Anybody?

      Thanks in advance for your help.

      Bert
        • 18913
        • 654 Posts
        Hi,
        Can you post your call to eForm?
        MattC
          • 3177
          • 137 Posts
          Yes, here it is:

          The call to eForm:
          [!contactform_Routines!]
          [!eForm? &formid=`contactForm` &to=`[email protected]` &tpl=`contact_Form` &report=`contact_Report` &thankyou=`contact_Thanks` &vericode=`0` &subject=`mySubject` &language=`nederlands` &eFormOnBeforeMailSent=`contact_OnBeforeMailSent` &eFormOnBeforeFormParse=`contact_OnBeforeFormParse` &protectSubmit=`0` !]
          

          the fact that I do get the IP address means that the function contact_OnBeforeMailSent is called and runs successfully.

          The function contact_OnBeforeFormParse is empty (and for future use):
          
          if(!function_exists('contact_OnBeforeFormParse')) {
          function contact_OnBeforeFormParse(&$fields, &$templates) {
          	
          	global $modx;
          	
          	
          	
          	return true;
          } // End Function
          } // End If function exists
          


          Hope you can help me.

          Thanks,

          Bert

            • 18913
            • 654 Posts
            When I change this
            $x = trim(urldecode($_REQUEST["x"]));
            

            to
            		//$x = trim(urldecode($_REQUEST["x"]));
                            $x = $_GET['id']; //let's get the id of the page calling this
            

            I seem to get the right info. Can you try that and see if it works?
            MattC
              • 3177
              • 137 Posts
              Hello,

              I have changed the $_REQUEST to a $_GET, but no succes here.
              Thinking about it, I can imagine it doesn’t work since the OnBeforeMailSent is a POST request further away. The end-user clicked on a "Send" button on which a POST Request is fired. So how would the form remember the GET variable then?
              I cannot understand it works on mconsidine’s installation.

              I have also tried to move the code, which gets the request variable, to the OnBeforeFormParse. That seems more logical to me since that is fired in the first request, where the GET variable is available.
              The value is put in the $fields array and a hidden field with that name is created. That one should be passed in the POST request.
              If doing that, the eForm returns with the error:
              Some errors were detected in your form:
              [undefined] » Tampering attempt detected!
              

              This would mean that no fields can be added in the $fields array. How can one work around this "tampering" error?

              Thanks for your help,

              Bert

                • 18913
                • 654 Posts
                Hmmm. I may not have understood you correctly. So let me look again ...
                MattC
                  • 18913
                  • 654 Posts
                  Okay, I’m puzzled too. The problem with my test is that I tested it with only "id=" as the parameter.

                  But when I try a line like this
                  http://www.example.com/index.php?id=12&x=test1&y=test2

                  I can’t get at anything past "id".

                  If I change my "onbefore" snippet to this
                  <?php
                  global $x,$y;
                  $x = $_GET["x"];
                  $y = $_GET["y"];
                  echo $x.'<br/>'.$y.'<br/>';
                  $y .= 'examplestring';
                  echo $y.'<br/>';
                  
                  if(!function_exists('my_OnBeforeMailSent')) {
                  
                  	function my_OnBeforeMailSent(&$fields) {
                  
                  		global $modx, $x, $y;
                  	
                  		// *** Get IP
                  		$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
                                  $fields['ip'] = $ip;
                  
                  		// *** Add data to the fields array
                  		$fields['x'] = $x;
                  		$fields['y'] = $y;
                  
                  		return true;
                  		} // End Function
                  		
                  } // End If Function Exists
                  ?>
                  


                  I get the expected output from the "echo" statements :
                  test1
                  test2
                  test2examplestring


                  But in the email that is sent to me, "x" is blank and "y" is only "examplestring". I’d be interested in hearing an explanation if anyone has one.
                  MattC
                  (ps this is under Evo 1.0.2)
                    • 18913
                    • 654 Posts
                    This works, though I hope someone can point to a cleaner solution. (The tempy, tempx approach came from a PHP site, so I can’t claim much credit for this.)

                    <?php
                    if(!function_exists('my_OnBeforeMailSent')) {
                    
                    	function my_OnBeforeMailSent(&$fields) {
                    
                    		global $modx;
                    	
                    		// *** Get IP
                    		$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
                                    $fields['ip'] = $ip;
                    
                                    $tempx = explode('&', html_entity_decode(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY)));
                                    $tempy = array();
                                    foreach($tempx as $val)
                                     {
                                       $tempz = explode('=', $val);
                                       $tempy[$tempz[0]] = $tempz[1];
                                     }
                                     unset($val, $tempz, $tempx);
                    
                    		// *** Add data to the fields array
                    		$fields['x'] = $tempy['x'];
                    		$fields['y'] = $tempy['y'];
                    
                    //die(print_r($_SERVER));
                    
                    		return true;
                    		} // End Function
                    		
                    } // End If Function Exists
                    ?>
                    
                      • 3177
                      • 137 Posts
                      Hello mconsidine,

                      That worked!! Thanks for your effort.

                      It does leave us with what looks like a MODX bug.

                      But this topic has enough clues for Google to pick it up so others can see the workaround.

                      Thanks,

                      Bert