21475 Posts in 5781 Topics by 2620 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1731 Views |
-
Contact Page Type

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 =)
-
Re: Contact Page Type

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> -
Re: Contact Page Type

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')); -
Re: Contact Page Type

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.phpThink 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
| 1731 Views | ||
|
Page:
1
|
Go to Top |

