dotCMS 1.7 has fairly straight-forward form handling that makes it easy to write (in HTML) a form that will submit to dotCMS (
action="/dotCMS/submitWebForm") and email the form contents to an email address you nominate. The purpose of this posting is to show the following.
- How to create a simple dotCMS form using this mechanism that emails the results to someone.
- How to encrypt the email addresses so that they do not appear in plain text within your HTML
- How to use some simple jQuery to make the form a "floating dialog" that can be triggered by clicking on a button.
Since I don't have a dotCMS server of my own, here are screen shots of the results.
This is what the form looks like.
Click the button and the form is displayed. It isn't a pop-up (those are evil); it's a hidden element that gets shown (less evil) when you click "Give Feedback". After the user has filled out the form and clicks "Submit", they are redirected back to the same page.
And this is the email you get from dotCMS containing details of the submitted form.
Next I will show the code in chunks, with a bit of discussion for each chunk.
The first things you need are imports for the jQuery theme, jQuery API itself and then jQuery UI.
What's with the
jsapi?key and the imports from
API key specific to your site so they know ... whatever it is they want to know. Read more about this on the Google Libraries API - Developer's Guide. This is perfect for things like Blogger i.e. services for which you cannot upload your own scripts or other arbitrary files.
Here is the HTML for the visible portion of the page.
<div style="border:1px solid;padding:10px;margin:10px;width:200px;"> <p>My very interesting page.. </p> <button id="opener">Give Feedback</button> </div>
This is the HTML for the dialog that will appear when the user clicks the button.
<form method="post" id="feedbackForm" name="feedbackForm" action="/dotCMS/submitWebForm" style="display:none;"> <input type="hidden" name="from" id="from" value="email@example.com"> #encrypt('firstname.lastname@example.org') #set ($to = $encryptedStr) <input type="hidden" name="to" id="to" value="$to"/> <input type="hidden" name="subject" id="subject" value="Feedback on your cool site"/> <input type="hidden" name="returnUrl" id="returnUrl" value=""/> <input type="hidden" name="errorURL" id="errorURL" value="/error/page" /> <input type="hidden" name="html" id="html" value="1" /> <input type="hidden" name="order" id="order" value="referrer,reason,feedback" /> <input type="hidden" name="formType" id="formType" value="feedbackForm" /> <fieldset> <h1>Feedback Form</h1> <p>Please use this form to provide feeback.</p> <input type="hidden" id="referrer" name="Referrer URL" value="" /> <p>Reason for feeback</p> <select id="reason" name="Reason for Feedback"> <option value="Your site is awesome!">Your site is awesome!</option> <option value="Are you my father?">Are you my father?</option> </select> <p>Comments</p> <textarea id="feedback" name="Feedback"></textarea> <p><input type="submit" value="Submit" /></p> </fieldset> </form>
Notes about this code.
This form submits to
/dotCMS/submitWebForm - which is the dotCMS code that will process the data and email it out to whoever is specified in the hidden
It is very easy to encypt the email address that will appear in the hidden
to field. Do it with the code below.
#encrypt('email@example.com') #set ($to = $encryptedStr) <input type="hidden" name="to" id="to" value="$to"/>
This is how the HTML looks on the browser:
<input type="hidden" value="aJ8pVupCRaDYMRzM8qXtCS4IGXAInnAtgKPynQZNDe4=" id="to" name="to">
/dotCMS/submitWebForm automagically decrypts it to get the actual email address(es).
returnUrl field specifies what URL the browser will go to after the form is submitted (shown below). The jQuery discussed further down sets this value.
<input type="hidden" name="returnUrl" id="returnUrl" value=""/>
Note that the hidden
returnUrl field is given the value of
window.location.href - so after clicking the button to submit the form, the browser will go back to the same page (the dialog form will not be visible again though; just like what happens if you refresh the page).
For a more detailed code example showing what options you can specify in the form, see the dotCMS Wiki page on Sample Form Code. See the dotCMS documentation page, Form Handling for an explanation of the different options. For example, you can even provide an alternate email template.
Current Form Handling. In dotCMS 1.9, there is a new way to build forms that means you don't need knowledge of HTML and makes the forms more re-usable as dotCMS Form entities. See the new Form Handling documentation for instructions on this.
I am not sure if the approach outlined in this post still works in 1.9. I am also not sure if the new approach hides emails. Or how easy it is to apply the jQuery touch.