SHOW:
|
|
- or go back to the newest paste.
| 1 | - | /*********************/ |
| 1 | + | _config.php |
| 2 | - | / _config.php ********/ |
| 2 | + | |
| 3 | - | /*********************/ |
| 3 | + | |
| 4 | // Send form catpcha result | |
| 5 | global $catpchaResult; | |
| 6 | $catpchaResult = -1; | |
| 7 | ||
| 8 | ||
| 9 | ||
| 10 | ||
| 11 | - | /*********************/ |
| 11 | + | ProductPage.php |
| 12 | - | / ProductPage.php ****/ |
| 12 | + | |
| 13 | - | /*********************/ |
| 13 | + | |
| 14 | class ProductPage_Controller extends Page_Controller {
| |
| 15 | ||
| 16 | /* Init variable */ | |
| 17 | public function initVariable() {
| |
| 18 | ||
| 19 | // Using global variable to save catpcha result | |
| 20 | global $catpchaResult; | |
| 21 | ||
| 22 | // Genere random numbers | |
| 23 | $randomNumberOne = rand(0, 100); | |
| 24 | $randomNumberTwo = rand(0, 100); | |
| 25 | ||
| 26 | // Saving result | |
| 27 | $catpchaResult = ($randomNumberOne + $randomNumberTwo); | |
| 28 | ||
| 29 | } | |
| 30 | ||
| 31 | public function CreateForm() {
| |
| 32 | global $catpchaResult; | |
| 33 | ||
| 34 | $this->initVariable(); | |
| 35 | ||
| 36 | $form = Form::create( | |
| 37 | $this, | |
| 38 | __FUNCTION__, | |
| 39 | FieldList::create( | |
| 40 | TextField::create('Name',''),
| |
| 41 | TextField::create('Surname',''),
| |
| 42 | EmailField::create('Email',''),
| |
| 43 | TextField::create('Address',''),
| |
| 44 | TextField::create('Phone',''),
| |
| 45 | TextField::create('Subject',''),
| |
| 46 | TextareaField::create('Message',''),
| |
| 47 | TextField::create('Captcha','')
| |
| 48 | ), | |
| 49 | FieldList::create( | |
| 50 | FormAction::create('handleForm','Send Form')
| |
| 51 | ->setUseButtonTag(true) | |
| 52 | ->addExtraClass('btn btn-default-color btn-lg')
| |
| 53 | ), | |
| 54 | RequiredFields::create('Name','Email','Phone', 'Message')
| |
| 55 | )->addExtraClass('form-style');
| |
| 56 | ||
| 57 | foreach($form->Fields() as $field) {
| |
| 58 | $field->addExtraClass('form-control')
| |
| 59 | ->setAttribute('placeholder', $field->getName().'*');
| |
| 60 | ||
| 61 | if($field->getName() == 'Captcha'){ $field->setAttribute('placeholder', 'Result: '.$catpchaResult); }
| |
| 62 | } | |
| 63 | ||
| 64 | $data = Session::get("FormData.{$form->getName()}.data");
| |
| 65 | ||
| 66 | return $data ? $form->loadDataFrom($data) : $form; | |
| 67 | } | |
| 68 | ||
| 69 | public function handleForm($data, $form) {
| |
| 70 | global $catpchaResult; | |
| 71 | ||
| 72 | Session::set("FormData.{$form->getName()}.data", $data);
| |
| 73 | ||
| 74 | $commentForm = MyForm::create(); | |
| 75 | $commentForm->ProductPageID = $this->ID; | |
| 76 | $form->saveInto($commentForm); | |
| 77 | $commentForm->write(); | |
| 78 | ||
| 79 | echo 'Inserted number: ' . $commentForm->Captcha; // Correct number | |
| 80 | echo 'Result ' . $catpchaResult; // Different number saved | |
| 81 | ||
| 82 | Session::clear("FormData.{$form->getName()}.data");
| |
| 83 | $form->sessionMessage('Thanks for your interest','good');
| |
| 84 | } | |
| 85 | ||
| 86 | } |