We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9439
    • 90 Posts
    I have a form, split into two pages, 1 and 2. The first two fields of the form are on page 1 and are validated externally through a lookup snippet. If they're validated, the user clicks a link to page 2 to complete the main form. (If they're not validated then the user needs to try again with different values or simply print out a form and post it.) So far, so good, works fine. But when the user's values validate, I want auto-populate the form on page 2 with the two values they've already entered on page 1. How do I do that?

    Formit Retriever seemed the obvious way, but it only works from a formit redirect. I have the values I need from the lookup snippet and I can stick them in a placeholder, but they're temporary. They show up through a chunk on page 1, but are just blank when I access them on page 2.

    So I'm thinking that I need to save the values as a session variable in the first lookup snippet, then in the form field on page 2, access a snippet that will lookup the saved session variable? Or something like that. Can someone help? It seems rather a simple thing to achieve, pass values from one form on one page to another form on a second page but I'm struggling.

    This question has been answered by multiple community members. See the first response.

      David Heriot
      pfsmedia
      • 5430
      • 247 Posts
      If I'm reading you right, which I may not be, it sounds like the first 'page' is a very simple form, simply checking against existing values before allowing the user to continue. If that's the case, and the second 'page' contains a completely different form (vs. using js to manipulate dom elements) just submit the first form to the second page and use a snippet on that page to grab the data from $_GET or $_POST (depending on your form method) and inject it into the proper inputs. If this isn't making sense I can describe it in greater detail, but if I'm not misunderstanding you this would be a pretty simple thing to do.
      • discuss.answer
        • 3749
        • 24,544 Posts
        If any of the values you need to forward are not from form fields on the first page, $_SESSION variables are probably the way to go.

        In the first page snippet:

        $_SESSION['field_name'] = 'value';


        On the second page, a snippet will have to get them and set placeholders:

        $modx->setPlaceholder('field_name', $_SESSION['field_name']);
          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
          • 9439
          • 90 Posts
          Hello claytonk, no, don't think you're misunderstanding, but I don't want to submit the form directly to the second page as the user should a) know that their details are validated and that they can click to page 2, or b) they can't proceed and need to take other actions.

          However, once on page 2, yes, using $_POST should work I think. I can just about get by with coding these things in PHP but getting it into a snippet just takes way longer for me!
            David Heriot
            pfsmedia
            • 9439
            • 90 Posts
            Hello Bob, yes, I see that creating that snippet will do the job, but how does the snippet fit into formit? User clicks to empty form on page 2 but the two fields from page 1 should auto-populate on the form on page 2. The snippet needs to execute on the page opening before the formit submission. How do I get the snippet to fire on opening the page?
              David Heriot
              pfsmedia
              • 5430
              • 247 Posts
              If you'd like to share what/how your checking existing data on the first form I'd be happy to write you up a solution. There are a dozen ways to do something like this, but the details make all the difference. In a situation like this I would typically write a snippet for the first page that displays different content (form or confirmation/error message) depending on the result of the data check. Successful confirmation could store the variables in $_SESSION as Bob suggested, or pass them along in $_GET or $_POST to the next form when the user clicks the continue option. I think the key here is what your checking for and how you're checking.
              • discuss.answer
                • 3749
                • 24,544 Posts
                I think just putting the snippet anywhere on the page would do it. I don't actually use FormIt, so I'm not sure how it handles the form, but I think if you put tags for the checked feature into the form, FormIt would leave them alone, so the placeholders you set in the snippet would replace that tag. You'll need a different tag for each checkbox:

                <input type="checkbox" [[+is_checked11]] ...
                <input type="checkbox" [[+is_checked12]] ...


                Your snippet on the first page can set the placeholder values for each field to 1 or 0.
                  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
                  • 9439
                  • 90 Posts
                  Sorry both, been away. Thank you for your suggestions. I used Bob's suggestion and it worked just fine.

                  Just for completeness, here's the code. At the end of page 1 lookup snippet:

                  $_SESSION['name1'] = $var1;
                  $_SESSION['name2'] = $var2;


                  Then, on page 2 form, added a little snippet at the top of the page before the form:

                  $modx->setPlaceholder('name1', $_SESSION['name1']);
                  $modx->setPlaceholder('name2', $_SESSION['name2']);
                  


                  and used the names in the input values

                  <input type="number"  value="[[+name1]]" />
                  <input type="number"  value="[[+name2]]" />


                  Looks pretty straightforward now, seeing it written down. Ah well.
                    David Heriot
                    pfsmedia