Jump to:

23453 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » contact page problem

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

Page: 1
Go to End
Author Topic: 839 Views
  • Harley
    Avatar
    Community Member
    153 Posts

    contact page problem Link to this post

    Hi,

    I have used the basic contact page from the tutorial which I got working fine. now I want to keep it on every page using an include but having difficulty making this work. I get my contact page to display but without the form.

    Here is the code

    <?php
    /* *****************
    * Model
    ******************/
    class ContactPage extends Page
    {
       static $db = array(
          'Mailto' => 'Varchar(100)', //Email address to send submissions to
          'SubmitText' => 'HTMLText' //Text presented after submitting message
       );
       
       //CMS fields
       function getCMSFields() {
          $fields = parent::getCMSFields();
       
          $fields->addFieldToTab("Root.Content.OnSubmission", new TextField('Mailto', 'Email submissions to'));   
          $fields->addFieldToTab("Root.Content.OnSubmission", new HTMLEditorField('SubmitText', 'Text on Submission'));   
       
          return $fields;   
       }

    }
    /* *****************
    * Controller
    ******************/
    class ContactPage_Controller extends Page_Controller{

       function ContactForm() {
       // Create fields
          $Params = Director::urlParams();
          
        $fields = new FieldSet(
           new TextField('Name', 'Name*'),
             new EmailField('Email', 'Email*'),
             new TextareaField('Comments','Message*')
          );
           
        // Create action
        $actions = new FieldSet(
           new FormAction('SendContactForm', 'Submit')
        );
          // Create action
          $validator = new RequiredFields('Name', 'Email', 'Comments');
       
             
        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";
       }
    }

    <% if GetContactPage %>
    <% control GetContactPage %>
    <% if Success %>
       $SubmitText
    <% else %>
       $Content
       $ContactForm
    <% end_if %>
    <% end_control %>
    <% end_if %>

    Regards

    Harley

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: contact page problem Link to this post

    Man, what is it with folks using $_REQUEST tonight? Cease and desist!

    Your code can be simplified if you check out some of the docs on Forms. There is built in posting back to a form that allows you to test and pass success message back without the tricks you're using in the template

    http://doc.silverstripe.org/doku.php?id=recipes:simplesignupform

    839 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.