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.

E-Commerce Modules /

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, Ed, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba

[SOLVED] Custom Form productpage needs to send Title of product with e-mail


Go to End


3 Posts   1587 Views

Avatar
JanJan

Community Member, 3 Posts

28 October 2011 at 9:36pm

Hello,

I have edited product.php and added a simple contact form from ssbits.com, so on every product page there is a contact form.
The only problem is that i want the product title (and mayby some other variables like price etc) to send with the e-mail.
Im trying this by adding the product title to the value of a textfield and sending this textfield with the email.
The fields are sending the data that is inserted, expect the variable title(its not showing in the textfield). I dont really know how to add this variable to the form/data. Any help would be great.

Here is the code that I've added to product.php:

class Product_Controller extends Page_Controller {

static $allowed_actions = array(
'ReageerForm'
);

function ReageerForm() {
// Create fields
$fields = new FieldSet(
new TextField('Title', 'Title', $value = $Title),
new TextField('Bedrijfsnaam', 'Bedrijfsnaam'),
new TextField('Naam', 'Naam'),
new EmailField('Email', 'Email'),
new TextField('Telefoon', 'Telefoon'),
new TextField('Onderwerp', 'Onderwerp'),
new TextareaField('Bericht','Bericht')
);

// Create action
$actions = new FieldSet(
new FormAction('SendReageerForm', 'Verzenden')
);

// Create Validators
$validator = new RequiredFields('Naam', 'Telefoon', 'Onderwerp', 'Bericht', 'Email');

return new Form($this, 'ReageerForm', $fields, $actions, $validator);

}

function SendReageerForm($data, $form, $title) {

//Set data
$From = $data['Email'];
$To = $this->Mailto;
$To = "test@test.nl";
$Subject = "Reactie Product";
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('reageerEmail');
//populate template
$email->populateTemplate($data);
//send mail
$email->send();
//return to submitted message
Director::redirect($this->Link("?success=1"));
}

Thank you in advance for the help you can provide.

Avatar
Howard

Community Member, 215 Posts

29 October 2011 at 10:38am

Hi JanJan - welcome to the forum.

The way that usually solve this is by using a hidden field in the form like this:

function ContactForm() {

		if($currentPage = DataObject::get_by_id("SiteTree", $this->ID)){

		$fields = new FieldSet(
			new TextField('Name'),
			new EmailField('Email'),
			new TextareaField('Message'),
			new HiddenField ('Page', 'Page',
				$currentPage->Title
			)
		);
		$actions = new FieldSet( new FormAction('doContactForm', 'Submit'));
		$validator = new RequiredFields('Email', 'Message');

		return new Form($this, 'ContactForm', $fields, $actions, $validator);
	}}

Then in your email template you can just use $Page to display the title of the page that the form was displayed on.

Does that help?

Avatar
JanJan

Community Member, 3 Posts

2 November 2011 at 12:13am

Hi Howard,

Thank you for your quick reply!
The information you have provided me was enough and has helped me to solve my problem.
The if($currenpage) statement is what i needed for my contact form.

Thank you again for you help!