Creating a Simple Contact Form for Silverstripe Website

To create a simple contact form for Silverstripe, simply follow the steps below:

  1. Create ContactPage.php under mysite/code:
    class ContactPage extends Page
    	static $db = array(
    		'Mailto' => 'Varchar(100)', //Email address to send submissions to
    		'SubmitText' => 'HTMLText' //Text presented after submitting message
    	//CMS fields
    	function getCMSFields() 
    		$fields = parent::getCMSFields();
    		$fields->addFieldToTab("Root.Content.OnSubmission", new TextField('Mailto', 'Email submissions to'));	
    		$fields->addFieldToTab("Root.Content.OnSubmission", new HTMLEditorField('SubmitText', 'Text on Submission'));	
    		return $fields;	
    class ContactPage_Controller extends Page_Controller
    	//Define our form function as allowed
    	static $allowed_actions = array(
    	//The function which generates our form
    	function ContactForm() 
          	// Create fields
    	    $fields = new FieldSet(
    		    new TextField('Name', 'Name*'),
    			new EmailField('Email', 'Email*'),
    			new TextareaField('Comments','Comments*')
    	    // Create action
    	    $actions = new FieldSet(
    	    	new FormAction('SendContactForm', 'Send')
    		// Create action
    		$validator = new RequiredFields('Name', 'Email', 'Comments');
    	    return new Form($this, 'ContactForm', $fields, $actions, $validator);
    	//The function that handles our form submission
    	function SendContactForm($data, $form) 
    	 	//Set data
    		$From = $data['Email'];
    		$To = $this->Mailto;
    		$Subject = "Website Contact message";  	  
    		$email = new Email($From, $To, $Subject);
    		//set template
    		//populate template
    		//send mail
    	  	//return to submitted message
    		Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
    	//The function to test whether to display the Submit Text or not
    	public function Success()
    		return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
  2. Create into themes/yourtheme/templates/Email:
            <p style="font-size:1.4em;color:#666;" >The following message was submitted to your site by <a href="mailto:$Email">$Name:</a></p>
            <p style="font-size:1.4em;color:#222;" class="comments">$Comments</P> 
  3. Create into themes/yourtheme/Layout/:
    <div id="content" class="typography">
    	<% if Success %>
    	<% else %>
    	<% end_if %>
  4. Build database and flush using:

Looking for quality SilverStripe Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , | Posted under SilverStripe CMS | RSS 2.0

Author Spotlight

Teet Bergmann

Teet Bergmann

I'm a freelance web designer from Estonia. Mostly doing sites using Silverstripe, magento, prestashop, wordpress.

2 Comments on Creating a Simple Contact Form for Silverstripe Website

  1. Mike says:

    Thanks for this Teet. Would this be suitable to insert into a sidebar?

Leave a Reply

Your email address will not be published. Required fields are marked *