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
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
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.
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
-
☆ 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.
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*]~].