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 /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Form submit goes back to create form method - is that normal?

Go to End

2 Posts   1533 Views


Community Member, 33 Posts

22 October 2009 at 12:05am

Edited: 22/10/2009 12:24am


I have a form where a user can email a friend a link to a 'Guide' or a 'Success Story'. An ItemofInterest() method takes the type and id params from the $_GET array and depending on which type it is, gets the appropriate record.

So far so good - the email a friend page is shown with the title of the item (page) that the user thinks is of relevance, and hidden fields in the form also hold this value + the url for the friend to click on.

The problem I have is that when the user clicks on Submit, it seems to be running the form creation method rather than the SendEmail action and so it goes off to ItemOfInterest a second time and this time there are no $_GET values and it fails. Here is the code:

class EmailFriendPage extends Page {
    static $db = array();
    static $has_one = array();

    static $icon = 'themes/xxxxx/images/treeicons/email';

class EmailFriendPage_Controller extends Page_Controller {

    protected $_senderEmail = '';

     * Creates the Email a friend form
     * @return Form
    public function EmailFriendForm() {

        $record = $this->ItemOfInterest();

        switch ($record->ClassName) {
            case 'GuidePage':
                $link = 'guides';
            case 'StoryPage':
                $link = 'success-stories';

        $link = Director::absoluteBaseURL() . $link;

        $fields = new FieldSet(
            new TextField('sender_name', 'Your name *'),
            new EmailField('email', 'Your friend\'s email *'),
            new HiddenField('item_of_interest_title', '', $record->Title),
            new HiddenField('email_link', '', $link)

        $actions = new FieldSet(new FormAction("SendEmail", "Send Email"));

        $validator = new RequiredFields('sender_name', 'email');

        return new Form($this, "EmailFriendForm", $fields, $actions, $validator);

     * Returns the record of the type chosen by the user for emailing to a friend
     * @return DataObject
    public function ItemOfInterest() {

        if (!isset($_GET['type']) or !isset($_GET['id'])) {
            Director::redirect(Director::baseURL(). $this->URLSegment . '/');

        $type = $_GET['type'];
        $id = $_GET['id'];
        $id = (int)$id;

        switch ($type) {
            case 'guide':
                $record = DataObject::get_by_id('GuidePage', $id);

            case 'story':
                $record = DataObject::get_by_id('StoryPage', $id);


        return $record;

     * Sends the email
     * @param array $data
     * @param Form $form
    public function SendEmail($data) {

        $status = '';

        try {
            $from = $this->_senderEmail;
            $to = $data['email'];
            $subject = $data['sender_name'] . ' has sent you a link to the xxxxxxx website';
            $body = $data['sender_name'] . ' thinks that you may be interested in the following link: ' . $data['email_link'];

            $email = new Email($from, $to, $subject, $body);

            $status = 'success';
        } catch (Exception $e) {
            $status = 'error';

        Director::redirect(Director::baseURL(). $this->URLSegment . '/?success=' . $status);



The HTML for the form tag is:

<form id="Form_EmailFriendForm" action="/cms/email-a-friend/EmailFriendForm" method="post" enctype="application/x-www-form-urlencoded">

The SendEmail method is rather basic at the moment - I'll look into templates once I can actually get it to run! :)

Another quick question: In ItemOfInterest(), if there is no type or id param, the code does go into the if block and the Director::redirect() code is stopped over, but nothing happens, so PHP throws an error on the next line after that, ie the $type = $_GET['type']; Any ideas why?

Please help! Many thanks in advance.

(Silverstripe 2.3.3, PHP 5.2.9, OS X 10.5)


Community Member, 33 Posts

22 October 2009 at 12:35am

OK, I've solved it. Not sure if it's a hack or not as I am still unclear as to the way SS processes requests, but I check for the existence of type and id url params, and if not present I then check in the $_POST vars instead and use those. If not present, head off to page not found.