I want to create a Contact form that has more than 4 fields in it, save the data into the database and email the result to the siteowner.
From what I've experienced, the built in Contact Form will only display four fields.
I have created a new Data Object and Page Type to perform the function:
CanIUpgradeProcess.php
class CanIUpgradeProcess extends DataObject {
static $db = array(
'EmailFrom' => 'Text',
'FirstName' => 'Text',
'Surname' => 'Text',
'MobileNumUpg'=>'Text',
'OptusContractNum'=>'Text',
'PrefContactNum'=>'Text',
'PrefContactTime1'=>'Text',
'PrefContactTime2'=>'Text',
'NewsLetter'=>'Text',
'Referrer'=>'Text'
);
}
Upgrade.php
class Upgrade extends Page {
static $add_action = "an Upgrade form";
static $icon = "cms/images/treeicons/task";
}
class Upgrade_Controller extends Page_Controller {
function CanIUpgrade() {
return new Form($this, "CanIUpgrade", new FieldSet(
// List the your fields here
new TextField("FirstName", "First Name"),
new TextField("Surname", "Last Name"),
new EmailField("EmailFrom", "Email address"),
new TextField("MobileNumUpg", "Number of Phone To Upgrade"),
new TextField("OptusContractNum", "Optus Contract # (If known)"),
new TextField("PrefContactNum", "Preferred Contact Number"),
new DropdownField("ContactTime1", "Preferred Contact Time", $source = array(
"1" => "9am to 12pm",
"2" => "12pm to 3pm",
"3" => "3pm to 6pm",
"4" => "Anytime between 9am and 6pm"),
$value="4"
),
new DropdownField("ContactTime2", "Alternate Contact Time", $source = array(
"9am to 12pm" => "9am to 12pm",
"12pm to 3pm" => "12pm to 3pm",
"3pm to 6pm" => "3pm to 6pm",
"Anytime" => "Anytime between 9am and 6pm"),
$value="9am to 12pm"
),
new DropdownField("Newsletter", "Grab Updates?", $source = array(
"Yes" => "You Bet! Yes Please!",
"No" => "Not This Time Thanks"),
$value="Yes"
),
new HiddenField ("Referrer", "", $_SERVER['HTTP_REFERER'])
), new FieldSet(
// List the action buttons here
new FormAction("process", "Can I Upgrade?")
), new RequiredFields(
// List the required fields here: "Email", "FirstName"
));
}
function process( $data, $form ) {
$submittedForm = new CanIUpgradeProcess();
$form->saveInto($submittedForm);
$submittedForm->write();
$email = new Email('from@yourdomain.com', 'to@yourdomain.com', 'Can I Upgrade?');
foreach($data as $key=>$value) {
$body .= $key.' = '.$value."\r\n";
//echo $body;
}
$email->setBody($body);
$email->send();
$url = "home";
Director::redirect($url);
}
}
The form displays the way I want it to
The text fields: Firstname, Surname, EmailFrom, MobileNumUpg, OptusContractNum, Preferred ContactNum, Referrer are all written the to table correctly.
The dropdownfields and checkboxfieldset are not stored correctly - they are stored as null.
The form emails, but does not include data, just the field names.
It appears to redirect.
Two questions:
1. How do you write the contents of a submitted dropdownbox and checkboxfieldsets to the data object?
2. How do you email the results properly?
This example is based on the recipe found at http://docs.silverstripe.com/?id=recipes:forms