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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Contact Page Type


Reply


6 Posts   2272 Views

Avatar
Brady.Dyer

Community Member, 21 Posts

6 January 2009 at 11:58pm

Does anyone have a page type they can post up that has a contact form that has some form of spam protection on it?

I know you can easily enable the math protection to comments on pages, but not for a CMS-generated form.

If anyone has anything that gets around spam problems with forms created in the CMS, that would be great if you could share =)

Avatar
UncleCheese

Forum Moderator, 4096 Posts

7 January 2009 at 4:05am

I use this on all my sites. Not the sexiest thing in the world, but it works.

/mysite/code/ContactPage.php

<?php

class ContactPage extends Page
{
   static $db = array (
      'SuccessMessage' => 'HTMLText'
   );
   
   public function getCMSFields()
   {
      $f = parent::getCMSFields();
      $f->addFieldToTab("Root.Content.Success Message", new HTMLEditorField('SuccessMessage','Success message (to be displayed after a user submits the form'));
      return $f;
   }
   

}

class ContactPage_Controller extends Page_Controller
{
   public function ContactForm()
   {
      return new Form(
         $this,
         'ContactForm',
         new FieldSet(
            new TextField('Name','Your name'),
            new EmailField('Email','Your email address'),
            new TextField('Spam','Spam protection: <em>Is fire hot or cold?</em>'),
            new TextareaField('Message','Message','10','40')
         ),
         new FieldSet(
            new FormAction('doContactSubmit','Submit')
         ),
         new RequiredFields(
            'Message','Spam'
         )
      );
   }
   
   public function doContactSubmit($data,$form)
   {
      $data = $form->getData();
      if(trim(strtolower($data['Spam'])) != 'hot') {
         Director::redirect($this->Link('spam'));
      }
      else {
         $email = new Email();
         $email->to = 'myemail@addressl.com';
         $email->subject = 'My Website: Contact Form';
         $email->from = !empty($data['Email']) ? $data['Email'] : 'webmaster@mysite.com';
         $email->ss_template = "ContactPageEmail";
         $email->populateTemplate($data);
         $email->send();
         Director::redirect($this->Link('success'));
      }

   }
   
   public function Successful()
   {
      $p = Director::urlParams('Action');
      return isset($p['Action']) && $p['Action'] == 'success';
         
   }

   public function SpamError()
   {
      $p = Director::urlParams('Action');
      return isset($p['Action']) && $p['Action'] == 'spam';
         
   }

}

?>

/mysite/templates/Layout/ContactPage.ss

<div id="content">
   <% if Successful %>
      $SuccessMessage
   <% else_if SpamError %>
      <p>Sorry, you did not provide a valid answer to the spam protection.</p>
      $ContactForm
   <% else %>
      $Content
      $ContactForm
   <% end_if %>
</div>

/mysite/templates/email/ContactPageEmail.ss

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
</head>
<body>

<p class="body">
A user has submitted a contact form through your website. His/her info appears below.
</p>
<p class="body">
<strong>Name</strong>: $Name<br />
<strong>Email</strong>: $Email<br />
<strong>Message</strong>:
</p>
<p class="body">
$Message
</p>

</body>
</html>

Avatar
Brady.Dyer

Community Member, 21 Posts

7 January 2009 at 11:38am

Thanks for that =)

Have tried to get it to work, but there is an error when it tries to send out the email. Do i need to change anything else here:

$email = new Email();
$email->to = 'noreply@beautyworx.co.nz';
$email->subject = 'Beautyworx: Contact Form';
$email->from = !empty($data['Email']) ? $data['Email'] : 'noreply@beautyworx.co.nz';
$email->ss_template = "ContactPageEmail";
$email->populateTemplate($data);
$email->send();
Director::redirect($this->Link('success'));

Avatar
Brady.Dyer

Community Member, 21 Posts

13 January 2009 at 4:06pm

Am getting the following error:

FATAL ERROR: Missing argument 1 for Email::__construct(), called in /home/duplico/beautyworx/dev/beautyworx/code/ContactPage.php on line 48 and defined
At line 52 in /home/duplico/beautyworx/dev/sapphire/core/Email.php

Think it might have something to do with it not passing the forms details to the template and hence thinking the from field is blank, because if I change

$email->from = !empty($data['Email']) ? $data['Email'] : 'webmaster@mysite.com';

to

$email->from = 'webmaster@mysite.com';

it sends it - but then the contents of the email I get are empty - as if the template sending the email isnt getting the data correctly

Avatar
suganya

Community Member, 9 Posts

9 July 2014 at 4:39pm

Hi all,

Can anyone please help me. how to view the contact form after putting the code :-(

Avatar
thomas.paulson

Community Member, 86 Posts

9 July 2014 at 9:39pm

Hello Suganya

Make sure you rebuild (dev/build) the database, and flush the cache. ie flush=all,