Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _config.php
- // Send form catpcha result
- global $catpchaResult;
- $catpchaResult = -1;
- ProductPage.php
- class ProductPage_Controller extends Page_Controller {
- /* Init variable */
- public function initVariable() {
- // Using global variable to save catpcha result
- global $catpchaResult;
- // Genere random numbers
- $randomNumberOne = rand(0, 100);
- $randomNumberTwo = rand(0, 100);
- // Saving result
- $catpchaResult = ($randomNumberOne + $randomNumberTwo);
- }
- public function CreateForm() {
- global $catpchaResult;
- $this->initVariable();
- $form = Form::create(
- $this,
- __FUNCTION__,
- FieldList::create(
- TextField::create('Name',''),
- TextField::create('Surname',''),
- EmailField::create('Email',''),
- TextField::create('Address',''),
- TextField::create('Phone',''),
- TextField::create('Subject',''),
- TextareaField::create('Message',''),
- TextField::create('Captcha','')
- ),
- FieldList::create(
- FormAction::create('handleForm','Send Form')
- ->setUseButtonTag(true)
- ->addExtraClass('btn btn-default-color btn-lg')
- ),
- RequiredFields::create('Name','Email','Phone', 'Message')
- )->addExtraClass('form-style');
- foreach($form->Fields() as $field) {
- $field->addExtraClass('form-control')
- ->setAttribute('placeholder', $field->getName().'*');
- if($field->getName() == 'Captcha'){ $field->setAttribute('placeholder', 'Result: '.$catpchaResult); }
- }
- $data = Session::get("FormData.{$form->getName()}.data");
- return $data ? $form->loadDataFrom($data) : $form;
- }
- public function handleForm($data, $form) {
- global $catpchaResult;
- Session::set("FormData.{$form->getName()}.data", $data);
- $commentForm = MyForm::create();
- $commentForm->ProductPageID = $this->ID;
- $form->saveInto($commentForm);
- $commentForm->write();
- echo 'Inserted number: ' . $commentForm->Captcha; // Correct number
- echo 'Result ' . $catpchaResult; // Different number saved
- Session::clear("FormData.{$form->getName()}.data");
- $form->sessionMessage('Thanks for your interest','good');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement