Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Creating a new form, saving to table and emailing to site owner


Go to End


3 Posts   1991 Views

Avatar
Charly

Community Member, 6 Posts

7 April 2008 at 9:23am

Edited: 09/04/2008 7:30pm

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

Avatar
Charly

Community Member, 6 Posts

9 April 2008 at 7:29pm

I have resolved the email issue and the function now returns a very nicely formatted email.

I am still having problems saving the form data the to DataObject. This what I have so far

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 CheckboxSetField("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 MyEmail;
$email->populateTemplate($submittedForm);
$email->send();
$url = "thankyou";
Director::redirect($url);
}
}

All data except the Dropdownfields save in the DataObject Table. The Dropdownfields save as NULL (they do however, appear correctly in the email).

Any ideas?

Avatar
Phalkunz

Community Member, 69 Posts

28 July 2008 at 2:36pm

Edited: 28/07/2008 2:43pm

Hi Charly,

The field names in the dropdownfields are incorrect. they should be PrefContactTime1 and PrefContactTime2 instead of ContactTime1 and ContactTime2.