The basics of creating a customer form handler in PHP are:
When a custom action is assigned to a form through the <wt:form> tag, the actions assigned in Form Builder will not be used.
Step 1: Assign a custom action to a form
In the template, specify the form to display and create an action to take when the form is submitted:
<wt:form source="Contact Us Consumer" action="contactSubmit"/>
Step 2: Write and register a PHP function to handle the custom action
In a PHP file in the code folder, declare a function to process the form and then register the form handler:
class ContactUs {
/**
* This function will process contact us form submissions
*
*
**/
public static function processContactForm($action, $data) {
// $action should be set to contactSubmit,
// $action is passed in so you can use the same function to process multiple forms
// get the guid of the form which was submitted
$formGuid = (int)$GLOBALS["WT"]->getRequestValue("formGuid", 0);
if ($formGuid) {
// get the form object
$formNode = $GLOBALS["WT"]->getNode($formGuid);
if ($formNode && $formNode->m_typeName == "wtForm") {
// get the form data with labels rather than internal WebTemplate names
$data = $formNode->getFormDataFromRequest(true);
// now for example, create a contact with the form data
$attributes = Array();
$attributes["First Name"] = $data["First Name"];
$attributes["Last Name"] = $data["Last Name"];
$attributes["Email Address"] = $data["Email Address"];
$contacts = $GLOBALS["WT"]->getNode("/Contacts");
$contact = $contacts->createChild("Contact", $attributes);
// return the contact (non false value) to show the form was processed
}
}
// error processing the form, return false to indicate the form wasn't processed
return false;
}
}
WTForm::registerFormHandler("contactSubmit", Array("ContactUs", "processContactForm"));
To call the WebTemplate form handling function from within a custom form handler
class ContactUs {
/**
* This function will process contact us form submissions
*
*
**/
public static function processContactForm($action, $data) {
// $action should be set to contactSubmit,
// $action is passed in so you can use the same function to process multiple forms
// get the guid of the form which was submitted
$formGuid = (int)$GLOBALS["WT"]->getRequestValue("formGuid", 0);
if ($formGuid) {
// get the form object
$formNode = $GLOBALS["WT"]->getNode($formGuid);
if ($formNode && $formNode->m_typeName == "wtForm") {
// call the webtemplate form hander, true as the second parameter says not to call the custom form hander function
return $formNode->submit($data, true);
}
}
// error processing the form, return false to indicate the form wasn't processed
return false;
}
}
WTForm::registerFormHandler("contactSubmit", Array("ContactUs", "processContactForm"));