Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Form submit goes back to create form method - is that normal?

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

Page: 1
Go to End
Author Topic: 1240 Views
  • Sparrowhawk
    Avatar
    Community Member
    33 Posts

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

    Hi,

    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:

    <?php
    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 = 'noreply@xxxxxxx.org.uk';

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

    $record = $this->ItemOfInterest();

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

    $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);
    break;

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

    default:
    Director::redirect('page-not-found/');
    }

    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);
    $email->send();

    $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)

  • Sparrowhawk
    Avatar
    Community Member
    33 Posts

    Re: Form submit goes back to create form method - is that normal? Link to this post

    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.

    1240 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.