We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51216
    • 35 Posts
    I want a snippet that will insert the ID of the submitted form into the emailTpl and fiarTpl. I need it in both the subject and the body of the emails.

    I'm a noob when it comes to PHP. Is this the right way to do it?

    [[!FomitID]]


    <?php
    $output = '';
    $sql = "SELECT id FROM modx_formit_forms ORDER BY id DESC LIMIT 1";
    foreach ($modx->query($sql) as $row) {
        $output .= $row['id'];
    }
    
    print ($output);
      • 46886
      • 1,154 Posts
      I think you might be trying to reinvent the wheel, there are extras for email lists

      I am not a php expert myself, but that code looks like its pulling the entire database.

      So you've got a formit form, and this is like a user submission? and then its going in the database into a particular field?

      We can get you there, but you may need to explain more...

      if its a form what you want I think is a posthook, a command to the form to do something after the form is submitted
        • 17301
        • 932 Posts
        Haven't tried it myself but looks like BobRay attempted to answer a similar question here:

        https://forums.modx.com/thread/101552/insert-the-database-id-into-the-emails-via-formit-using-the-formsave-hook

          ■ email: [email protected] | ■ website: https://alienbuild.uk

          The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
          • 51216
          • 35 Posts
          Quote from: nuan88 at Aug 24, 2018, 12:22 PM
          I think you might be trying to reinvent the wheel, there are extras for email lists

          I am not a php expert myself, but that code looks like its pulling the entire database.

          So you've got a formit form, and this is like a user submission? and then its going in the database into a particular field?

          We can get you there, but you may need to explain more...

          if its a form what you want I think is a posthook, a command to the form to do something after the form is submitted

          Yes well I'm actually using AjaxForm. It's just a contact form. One email sent to the company and a copy sent to the submitter. I'm trying to put a unique identifier into the emails. I figured I could just use the ID from modx_formit_forms in the database because I'm using the FormItSaveForm hook.

          How would I go about creating a posthook?

          Here's the call:

          [[!AjaxForm?
              &form=`formTpl2`
              &snippet=`FormIt`
              &hooks=`recaptchav2,email,FormItSaveForm,FormItAutoResponder`
              &fieldNames=`name==name, email==email, message==message`
              &emailTpl=`emailTpl`
              &emailSubject=`[[++site_name]] Website Enquiry`
              &emailFrom=`[[+email]]`
              &emailFromName=`[[+name]]`
              &emailTo=`[[+addressTo]]`
              &emailReplyTo=`[[+email]]`
              &emailReplyToName=`[[+name]]`
              &fiarTpl=`emailFiarTpl`
              &fiarSubject=`[[++site_name]] Website Enquiry`
              &fiarFrom=`[[+addressTo]]`
              &fiarFromName=`[[++site_name]]`
              &fiarToField=`email`
              &fiarReplyTo=`[[+addressTo]]`
              &fiarReplyToName=`[[++site_name]]`
              &validate=`name:required,
                email:email:required,
                surname:required,
                contact_number,
                company_name,
                courseSelect,
                placeSelect,
                addressTo`
          ]]
            • 17301
            • 932 Posts
            To create a random number you could use the random number snippet found here:
            https://github.com/pdincubus/RandomSnippets/blob/master/snippet.RandomSnippets.RandomNumber.php

            Just create a hidden input with the value set to the random number. For example:
            <input type="hidden" name="identifier" value="[[!RandomNumber? &formatNumber=`1` &length=`5`]]">


            Then in your email you can just call [[+identifier]]

            Caveat there is a possibility (VERY tiny) that the numbers wont be unique, but of course the larger the length the less likely that it'll be generated again.
              ■ email: [email protected] | ■ website: https://alienbuild.uk

              The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
              • 3749
              • 24,544 Posts
              If you could explain why you want to send a unique identifier and what you plan to do with it, we could probably be more help.

              There is also PHP's uniqid() function which is guaranteed to be unique, but is not cryptographically secure.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 46886
                • 1,154 Posts
                @scoder ok good, just needed to know a bit more I thought. I think here the AjaxForm is going to email by itself, so a posthook seems not needed.

                Now, what do you have in your emailtpl? I am not positive, but this is an email for Register snippet, the syntax should work pretty much like this:

                <p>After activating the link, you may in the future login with this password and your username:</p>
                
                <p>
                Username: <strong>[[+username]]</strong><br />
                Password: <strong>[[+password]]</strong></p>


                Just insert the values you need.


                As for the issue LK mentioned, hmm thinking...how about a counter for AjaxForm, every time it fires the number goes up. Just spitballing tho
                  • 51216
                  • 35 Posts
                  Thanks to everyone for their input.

                  The RandomNumber snippet is good to know, I might use that in future for something else.

                  To explain further, my client has told me he wants to know the number of inquires he has had from the website. So sequential numbering is essential for him. If each email has a sequential number he can easily identify how many just by looking at the subject.

                  This why I used this my snippet:
                  SELECT id FROM modx_formit_forms ORDER BY id DESC LIMIT 1


                  Will it always get the right ID this way? Is there a better way of doing it?
                    • 3749
                    • 24,544 Posts
                    If the numbers are coming from an autoincrement field in the DB, they are guaranteed to be unique, but they are not guaranteed to be sequential. There may be gaps in the numbers.

                    It would be more reliable to store the number somewhere (TV value, file, snippet property, system setting, chunk, unused resource field, etc.) and increment it yourself in your plugin.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 46886
                      • 1,154 Posts
                      [[counter_tv_call]]

                      {{form}}

                      [[!snippet]]

                      TV holds the data, don't ask me how, form takes tv value for email, and has posthook upon success to fire the snippet, snippet adds one to the tv value.

                      Should work perfect, start from 1 though because its going to fire out the *current tv value not the new value. But that's manageable anyway

                      BobRay I think we talked about this one back in the Discuss days