I’ve recently migrated my website to CMS Made Simple, and something that I struggled to get working was the FormBuilder module. I had a contact form on my old site, so I wanted to bring that across to CMSMS. The problem is that it doesn’t support extensionless pretty URLs. I considered adding a .html or .php extension to the end of my URLs, but in the interest of keeping the URLs relevant and easily migratable in the future, I decided against it.
I was just about to give up and do it in PHP, but then I found a 4 year old forum post by calguy1000 describing how to submit forms using Smarty and User Defined Tags (UDTs).
Code for ‘cmsmailer’ UDT:
global $gCms;
$smarty = & $gCms->GetSmarty();
$cmsmailer = $gCms->GetModuleInstance('CMSMailer');
$smarty->assign_by_ref('cmsmailer',$cmsmailer);
You may notice that there’s a tiny difference between my UDT and calguy1000’s. His references the CMSMailer module by calling & $gCms->modules['CMSMailer']['object'];, but for me this caused a few errors so I checked the CMSMailer documentation and discovered that it should now be referenced like this: $gCms->GetModuleInstance('CMSMailer');. Basically, the UDT provides us with a way to access CMSMailer. We can use some Smarty functions to check POST values, and work out what needs to be displayed and whether the form was valid. If it’s all valid, we send the message - otherwise we display some validation errors.
Below is an example of how to use the UDT in a contact form. A lot of it is taken from calguy1000’s post, but I’ve made a few improvements to it. Remember to replace YOUR-EMAIL-ADDRESS@EXAMPLE.COM with the email address that you want to receive the form submission.
The easiest way to use this is to paste it in to a Global Content Block, and then reference that in the content of one of your pages.
{if isset($smarty.post.contactformsubmit)}
{$assign "recipient" "YOUR-EMAIL-ADDRESS@EXAMPLE.COM"}
{$assign "isValid" "true"}
<div class="validation-errors">
{* Check for any invalid responses. *}
{if empty($smarty.post.name)}
<p><font color="red">Please enter your name</font></p>
{$assign "isValid" "false"}
{/if}
{if empty($smarty.post.email)}
<p><font color="red">Please enter your email address</font></p>
{$assign "isValid" "false"}
{/if}
{if empty($smarty.post.subject)}
<p><font color="red">Please enter a subject</font></p>
{$assign "isValid" "false"}
{/if}
{if empty($smarty.post.message)}
<p><font color="red">Please enter a message</font></p>
{$assign "isValid" "false"}
{/if}
</div>
{* All good - send the message *}
{if $isValid != "false"}
<!--
{* prepare the message subject *}
{capture assign='subject'}Website form submission: {$smarty.post.subject}{/capture}
{* prepare the message body *}
{capture assign='body'}
Message From: {$smarty.post.name} ({$smarty.post.email})
Message Sent: {$smarty.now|cms_date_format}
Message Text:
{$smarty.post.message}
{/capture}
{* send it *}
{cmsmailer}
{$cmsmailer->reset()}
{$cmsmailer->AddAddress($recipient)}
{$cmsmailer->SetSubject($subject)}
{$cmsmailer->SetBody($body)}
{capture assign='junk'}{$cmsmailer->Send()}{/capture}
-->
<p>Thanks for getting in touch!</p>
{/if}
{/if}
{if $isValid == "false" || !isset($smarty.post.contactformsubmit)}
{* Display the form if it: 1. Wasn't valid; or 2. Hasn't been submitted yet. *}
<form name="contact-form" id="contact-form" method="post">
<div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="formfield" value="{if isset($smarty.post.name)}{$smarty.post.name}{/if}" />
</div>
<div>
<label for="email">Email:</label>
<input type="email" name="email" id="email" class="formfield" value="{if isset($smarty.post.email)}{$smarty.post.email}{/if}" />
</div>
<div>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" class="formfield" value="{if isset($smarty.post.subject)}{$smarty.post.subject}{/if}" />
</div>
<div>
<label for="message">Comments:</label>
<textarea name="message" id="message" rows="10" class="formfield">{if isset($smarty.post.message)}{$smarty.post.message}{/if}</textarea>
</div>
<div><input type="submit" name="contactformsubmit" id="submitbutton" value="Send"></div>
</form>
{/if}
Once you've got all that server-side stuff working, you can obviously add client-side validation for a smoother experience and a bit less work on the server.
See something like this in action on my about page.