It would be far easier to just use the SilverStripe Form API to create the form. Validation is also supplied.
Here's a code example of a "ContactPage" page type that has a basic contact form on it. This code would sit inside the ContactPage.php file, which should reside in the mysite/code directory for your project.
<?php
class ContactPage extends Page {
}
class ContactPage_Controller extends Page_Controller {
function Form() {
$fields = new FieldSet(
new TextField('Name', 'Your name'),
new EmailField('Email', 'Your email address'),
new TextareaField('Comments', 'Any comments?')
);
$actions = new FieldSet(
new FormAction('doSubmit', 'Submit')
);
$validator = new RequiredFields(
'Name',
'Email'
);
return new Form($this, 'Form', $fields, $actions, $validator);
}
function doSubmit($data, $form) {
$email = new Email();
$email->setTemplate('ContactPage_Email');
$email->setSubject('Someone submitted the contact form');
$email->setFrom('admin@somesite.com');
$email->setTo('admin@somesite.com')
$email->populateTemplate($data);
$email->send();
$form->sessionMessage('Thanks for contacting us. We'll endeavour to get in touch with you as soon as possible!');
Director::redirectBack();
}
}
?>
$Form is where your contact form will sit in the template when the user browses to the contact page. This is where the Form() function returns the form, with the appropriate fields, submit buttons and validation etc. If you're using the blackcandy theme (or a copy of it), you shouldn't have to do anything special, apart from just ensure that the "ContactPage" page type is set in the CMS for your contact page, using the dropdown in the "Behaviour" tab.
the doSubmit() function is where the email gets sent when the user submits the form. In this case, we set up an email to send to the site admin so that they know someone needs to be contacted.
You'll just need to create ContactPage_Email.ss inside the mysite/templates/email directory for your SilverStripe project. This email template can contain the variables $Name, $Email, $Comments etc which are substituted with what the user submitted in the contact form. The line $email->populateTemplate($data) facilitates this by pushing through the submitted form data into that email template.
Hope this helps!
Cheers,
Sean