We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8822
    • 203 Posts
    Does anyone know what the best way of passing the value of a TV on one page and populating the field in form (formit) on another page. To put it into context i am storing the name of a business in a TV (business-name). When a user clicks on 'Recommend Business' the ' recommend business' page is displayed and i want the form's 'Business Name' field to be populated with the name of the business. What's the best way of doing this? Many thanks [ed. note: Emily last edited this post 12 years, 11 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      If you're using FormIt you can use a pre-hook snippet to get the desired TV value and populate the field with it.

      http://bobsguides.com/revolution-objects.html
      http://rtfm.modx.com/extras/revo/formit/formit.hooks#FormIt.Hooks-UsingpreHooks
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 37261 ☆ A M B ☆
        • 113 Posts
        A simple method would be to use a snippet called before Formit to place a GET variable into a placeholder:

        <?php
        $modx->setPlaceholder('company',$_GET['company']);
        return '';
        


        Then you can put that placeholder wherever you need it in the form. I assume MODX properly sanitizes setPlaceholder but you can perform additional sanitizing operations before setting the placeholder in the snippet. [ed. note: pyrographics last edited this post 12 years, 11 months ago.]
          • 25803 ☆ A M B ☆
          • 721 Posts
          I believe I've done this with SetPlaceholders.
            • 8822
            • 203 Posts
            Thanks for the info guys - i've read all the documentation and your links etc. but am not as techy savvy as you guys and can't figure out how it all fits together.....

            I can populate the required field using a preHook with a static value, i.e.

            <?php
            $hook->setValue('business-name','This is supposed to be the company name');
            return true;
            


            but what is the correct syntax for using SetPlaceholders to store the TV value from the previous page and how do i reference this in the setValue call??

            Appreciate your help!
              • 25803 ☆ A M B ☆
              • 721 Posts
              You might also try fastField. For example, the syntax would be:

              <input type="text" value="[[#1.tv.business-name]]" whatever else you have for input parameters />

              Where the 1 in #1 is document ID you're grabbing the tv's value from.
                • 35150 ☆ A M B ☆
                • 191 Posts
                FormIt is a little tricky, since how the value is passed in initially will be different than how it's set when the form reloads (like when you submit it but some things don't pass validation so the form is redisplayed with error messages).

                A few questions:

                • Is this a field which the user can edit, you just want it pre-populated with a business name?
                • Is the business name coming from a TV on one particular page, or does it change based on the last page the visitor was on?

                If the answers are 'Yes', and 'based on the last page', then something like this ought to work.
                On the first page—the one before the form—link to the form like this:
                <a href="[[~13]]?bn=[[*businesstv:urlencode]]">Form</a>

                Change 13 to be the id of the resource where your form is and businesstv to be the name of your 'business name' tv, whatever that is. That will pass the business name to the form as a GET parameter.

                Then in your form, put this at the top:
                [[!setPlaceholders? &ph=`business == request.bn`]]

                This gets bn from either $_GET or $_POST. When a visitor clicks the link to go to the form bn will be in $_GET. If the user submits the form but it's returned because of validation problems, bn will be in $_POST. This line covers both cases.

                And the form field will look something like this:
                <input type="text" id="bn" name="bn" value="[[+business]]">
                  Extras :: pThumb • Resizer • imageSlim • setPlaceholders
                  • 8822
                  • 203 Posts
                  Hey Guys, thanks so much for your responses.

                  jgrant, massive thank you to you - you have completely solved my problem !! Just for completeness, i had to change

                  	<a href="[[~13]]&bn=[[*businesstv:urlencode]]">Form</a>

                  to
                  	<a href="[[~13]]?bn=[[*businesstv:urlencode]]">Form</a>


                  Thank you smiley
                    • 35150 ☆ A M B ☆
                    • 191 Posts
                    Cool, glad it helped!
                    Yes, you're right about the ? instead of the & in the query string.
                      Extras :: pThumb • Resizer • imageSlim • setPlaceholders
                      • 2396
                      • 101 Posts
                      I hope this isn't too off topic but this flexibility is why I love MODX. Following rx2's suggestion above, I used the fastField extra to do something similar – pass an input field value from a simple subscribe form on one page to an input field in a more complex subscribe form on another page, so much easier and cleaner than manually doing it with js or php – it would work the same for passing a TV value I assume.

                      <form action="[[~mysubscribepageID]]" method="post" name="subscribeto" target="_parent">
                      <input id="subscribeto" type="email" name="subscribeto" value="Enter email address..." />
                      <input id="subscribebutton" class="button" type="submit" name="subscribe" value="Subscribe now" />
                      </form>


                      into a field on another page

                      <form>...
                      <input id="form-to" class="required email" type="text" name="emailaddress" value="[[!#post.subscribeto:stripTags]]" maxlength="100" />
                      ...</form>