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

    I have an eform contact us page setup with a drop-down list of names and corresponding id values (rather than email addresses). I want to query the MODx database with the selected option id after a form submission and pull out the corresponding email address. I know how to get the email address from the db but I’m not sure how/where to modify the eform code to accomplish this.

    Any suggestions?

    Thanks
      • 10896
      • 76 Posts
      Ok. I still haven’t solved this but I think I’m making progress. Here’s where I am so far...

      Here’s my snippet call. I’m trying to just output to the browser to test so that’s why I have the noemail and thankyou is same as report.

      [[eformEvent01]]
      
      [[eForm? &formid=`feedbackForm` &to=`[email protected]` &replyto=`email` &tpl=`eContactUsForm` &report=`eContactUsReport` &subject=`Message from website contact form` &eformOnBeforeMailSent=`getEmailAddress` &noemail=`1` &thankyou=`eContactUsReport` ]]
      


      The key thing I discovered was eformOnBeforeMailSent. But something isn’t working as the resulting page after form submission is blank. Here is my snippet code:

      <?php
      
      function getEmailAddress( &$fields ) {
      		
          	$id = $fields['staffmember'];
          	
              // get email address
              $result = $modx->db->select('value', 'modx_site_tmplvar_contentvalues', 'contentid = '. $id . ' AND tmplvarid = 49');
              while($row = $modx->db->getRow($result)) {
                  $fields['to'] = $row['value'];
              }
      
      		return true;
      	}
      ?>
      


      Note: I’m not sure if $field[’to’] is the correct way to replace the to field for this snippet.

      Any thoughts?
        • 10487 MODX Staff
        • 1,535 Posts
        I’m not up to speed on eForm but I think you will need to make $modx global in that function (or pass it into the function as a reference) - it’s most likely failing at the point you call on the $modx object. Try adding this as the first line in your function:
        global $modx;
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 10896
          • 76 Posts
          Garryn,

          Thanks for the help. Unfortunately that didn’t help accomplish what I’m attempting to do. I think my problem is in this line of code. I’m not sure how to actually reference the TO field for this snippet

          $fields['to'] = $row['value'];
          


          Does anyone else have any ideas?
            • 30223
            • 1,010 Posts
            The ’to’ that you’re referring to is not a form field but a snippet parameter (it’s not in the $fields array). You don’t have access to the snippet parameters in your function but there is a way to get around that in this case.

            You can set the &to parameter to a placeholder which can be filled with one of the fields in your form. Try this:

            [!eForm? &to=`[+staffmember+]` &eformOnBeforeMailSent=`getEmailAddress` ..... !]


            and change your function to this:
            <?php
            function getEmailAddress( &$fields ) {
            		
                	$id = $fields['staffmember'];
                	
                    // get email address
                    $result = $modx->db->select('value', 'modx_site_tmplvar_contentvalues', 'contentid = '. $id . ' AND tmplvarid = 49');
                    while($row = $modx->db->getRow($result)) {
                        $fields['staffmember'] = $row['value'];
                    }
            
            		return true;
            	}
            ?>


            This will of course destroy your original ’staffmember’ value. If that bothers you create an extra (hidden) field with a dummy value in your form and use that instead. Also make sure you don’t use PHX with this setup as the placeholder in the snippet call will be destroyed by PHX before it’s of any use to you.
              • 10896
              • 76 Posts
              Thanks everyone,

              It appears that I’ve got it to work. I figured I’d post my code for future reference for others:

              eFormEvent01

              <?php
              function getEmailAddress( &$fields ){
              
                  global $modx;
                  $id = $fields['staffmember'];
                  	
                  // get email address
                  $result = $modx->db->select('value', 'modx_site_tmplvar_contentvalues', 'contentid = '. $id . ' AND tmplvarid = 49');
                  while($row = $modx->db->getRow($result)) {
                      $fields['staffmember'] = $row['value'];
                  }
              
                  return true;
                  
              }
              
              //return empty string
              return '';
              ?>
              


              Contact Us Page Content Field

              [!eformEvent01!]
              
              [!eForm? &formid=`feedbackForm` &to=`[+staffmember+]` &replyto=`email` &tpl=`eContactUsForm` &report=`eContactUsReport` &subject=`Message from website contact form` &thankyou=`eContactUsThanks` &vericode=`1` &eFormOnBeforeMailSent=`getEmailAddress` !]