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.

Form Questions /

TextField with $var - simple Captcha?


Reply


1259 Views

Avatar
yaizo

Community Member, 19 Posts

14 April 2010 at 5:09am

Edited: 14/04/2010 5:12am

hello folkz,

following this tutorial -> http://www.ssbits.com/creating-a-simple-contact-form i have made a new form in my page.
this works very well but what i need is one further TextField('SpamProtect') witch is given an variable.
normally i would put some vars and if else query but i don't know how to put this in here??? because this is an db-Field array?

so this is the code i use: mysite/code/ContactForm.php

<?php
class ContactPage extends Page{

static $db = array(
'Mailto' => 'Varchar(100)',
'SubmitText' => 'Text'
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.OnSubmission", new TextField('Mailto', 'Email submissions to'));
$fields->addFieldToTab("Root.Content.OnSubmission", new TextareaField('SubmitText', 'Text on Submission'));

return $fields;
}
}

class ContactPage_Controller extends Page_Controller{

function ContactForm() {
// Create fields
$fields = new FieldSet(
new TextField('Name', 'Name*'),
new EmailField('Email', 'Email*'),
new TextareaField('Message','Ihr Anliegen*'),
//the next Fild i set is my Spam protector,this here must given an variable but how?
new TextField('SpamProtect', 'Anti-Spam*')
);

// Create action
$actions = new FieldSet(
new FormAction('SendContactForm', 'Send')
);

// Create Validators
$validator = new RequiredFields('Name', 'Email', 'Message', 'SpamProtect');

//i think here must be an variable witch is connected with 'SpamProtect'
$spamProtector = "anti-spam-code-hier";

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

}

function SendContactForm($data) {

//Set data
$From = $data['Email'];
$To = $this->Mailto;
$Subject = "Website Contact message";
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('ContactEmail');
//populate template
$email->populateTemplate($data);
//send mail
$email->send();
//return to submitted message
Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
}

public function Success(){
return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";

//here have to be a further query: success ==1 and $spamProtector == "anti-spam-code" -> send message.
???
return (isset($_REQUEST['success'] == "1) && ($spamProtector == "???SpamProtector???"));
???
}
}
?>

if anyone can give me an helping hand i would be very thankful.
ps. i tested userforms and mellom an so on, but this things are to big for me and my know-how.
0.O m.the.green