We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10746
    • 126 Posts
    OK, now I have ground to a halt and realise that I need some help as to how to proceed...

    I am trying to write a snippet that will

    - create a simple HTML form with checkboxes, textfields and so on
    - on submission, process the form, do the DB stuff referred to in another thread and output the results.

    But I am gummed up on how (and where) to produce the form, and how to make it refer to "the current document" as its target... I have tried to work through some of the existing form-ish snippets but they are pretty sophisticated.

    I can see some options, but would really appreciate expert advice.. So should i

    (1) use a PHP function to actually generate the entire form (as a giant string) and then "fill in" the current document ID using some modx function

    (2) Create the HTML for the form in a chunk, and then use the snippet to customise .. this seems like a nice thing, but I have no idea of what to actually do to make a snippet access a chunk... (this is probably something I should learn.. where?)

    (3) Any other way?

    Thanks

    Gordon


      • 30223
      • 1,010 Posts
      Have a good look at eForm. eForm can be extended using it’s event structure which in your case you could use to do the ’DB stuff’. There’s good documentation and a few examples that should get you going. It’ll take you a lot less time to get your head around eForm then building yet another form handler.. you can then concentrate on getting your DB stuff working.



        • 15987
        • 786 Posts
        Gordon,
        Like Tobyl said, eform is probably your best bet, but here are some of the answers to your questions.

        First let me start off with the easy one, to have the form post back to the same page just use [~id~] as the action (i.e. action="[~id~]")

        Second, to create the form you have 2 different options.

        1) Like you said just build the form into a string, placing the above action in the correct place. Then just return the string.

        2) If you want to use a chunk and placeholders here is what I typically do:

        1. create a chunk with the layout and placeholders like [+some placeholder+] for any content that needs dynamically generated.
        2. in your snippet call $myChunk = $modx->getChunk(’chunk name’); This will return the chunk as a string if found
        3. next create an array of placeholders, i.e. $myPlaceholders = array(’[+ph1+]’,’[+ph2+]’);
        4. then create another array with the values you want to insert into the placeholders, i.e. $myValues = array(1,2);
        5. next process the chunk and add it to the output, $output .= str_replace($myPlaceholders,$myValues,$myChunk);
        6. return the $output variable and you will have your form.

        Hope that helps
          • 22303 MODX Staff
          • 10,725 Posts
          Or MakeForm as well...though it takes a little more coding, you have a little more control over the form generation and processing. It will also be tightly integrated in xPDO, with support for presenting and processing forms directly via objects.

          It’s just a class to help in writing such snippets however; not a pre-made snippet solution like eForm that just takes a bunch of configuration parameters.
            • 10746
            • 126 Posts
            Quote from: TobyL at Nov 28, 2006, 07:12 AM

            Have a good look at eForm. eForm can be extended using it’s event structure which in your case you could use to do the ’DB stuff’.

            I did look at the main repository page for eForm, but the tagline "A snippet that will let you convert a web form into an email" led me to believe that it was not what I wanted for this particular task, as email is not relevant for this particular desired snippet. (In fact I already use eForm for a "Contact Me" form.)

            But I will have a look again and see if I can understand more about it..

            Cheers

            Gordon
              • 10746
              • 126 Posts
              Quote from: kylej at Nov 28, 2006, 07:20 AM

              2) If you want to use a chunk and placeholders here is what I typically do:
              1. create a chunk with the layout and placeholders like [+some placeholder+] for any content that needs dynamically generated.
              2. in your snippet call $myChunk = $modx->getChunk(’chunk name’); This will return the chunk as a string if found
              3. next create an array of placeholders, i.e. $myPlaceholders = array(’[+ph1+]’,’[+ph2+]’);
              4. then create another array with the values you want to insert into the placeholders, i.e. $myValues = array(1,2);
              5. next process the chunk and add it to the output, $output .= str_replace($myPlaceholders,$myValues,$myChunk);
              6. return the $output variable and you will have your form.

              Great! Thanks heaps for that...

              Exactly what I needed to know about accessing chunks from snippets... do I have to make sure that my placeholders don’t get mixed up with other placeholders?

              One extra question... I am slightly confused by the concept of "return $output" from the snippet.. from my own observations, I have noticed that if my snippet just generates output (via print / echo statements) then this output seems to appear in the correct place... If a snippet "returns" something, then that implies the existence of something that "calls" the snippet and does something with the returned value...

              Can anyone explain the difference between a snippet that PRODUCES some output and one that RETURNS its output?

              Thanks

              Gordon

                • 15987
                • 786 Posts
                Gordon,
                When a snippet returns something it is outputted where the snippet is called. Typically what I do with all of my snippets is to generate the variable $output and then return that. It gives you better control of where things are displayed by the snippet. This also allows you to set the output to a placeholder. For example, if you don’t want wayfinder to be displayed where the snippet is called you can set the output to a placeholder and then put the placeholder anywhere on the page to display the menu.

                There could also be instances where a snippet is called inside another snippet. If items are echoed in mid-execution it could cause problems for the nesting snippet.

                I think it is a best practice to always have your snippet return the output.

                For placehoders, yes you should make sure they are not mixed up with others. One way of doing this is by doing something like [+wf.example+], [+wf.example2+] that is what I use for wayfinder. you could even use the whole snippet name if needed.
                  • 10746
                  • 126 Posts
                  First let me start off with the easy one, to have the form post back to the same page just use [~id~] as the action (i.e. action="[~id~]")

                  Should this be

                  action = "[~[*id*]~]"


                  If instead of this, I used a placeholder [+myURL+] and then did a replace of [+myURL+] with $modx->makeURL($modx->documentIdentifer), would this require fewer passes through the parser?

                  My (still naive and incomplete) understanding is that the first time through, my snippet would create the form and leave the [~[*id*]~] inside the text, then it would be re-parsed and the [*id*] replaced with the id, and then it would be re-re-parsed and the [~17~] (or whatever) would be replaced with the actual URL...

                  If that is true, then my snippet could bypass all that by actually filling in the URL itself...

                  Cheers

                  Gordon
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Actually, you can just use action="" and it will automatically be returned to the page it originally came from.
                      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
                      • 33372
                      • 1,611 Posts
                      Quote from: sottwell at Nov 29, 2006, 12:42 AM

                      Actually, you can just use action="" and it will automatically be returned to the page it originally came from.
                      I always thought that, too, but recently Ryan told me that Safari won’t submit the form at all if the form action is empty so now I always use [~[*id*]~].
                        "Things are not what they appear to be; nor are they otherwise." - Buddha

                        "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options