Jump to:

1976 Posts in 1306 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Retrieving Session data

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

Page: 1
Go to End
Author Topic: 539 Views
  • Chilli-D
    Avatar
    Community Member
    5 Posts

    Retrieving Session data Link to this post

    I have a simple form which I need the user to enter a postcode and that information then display on a specific template page.

    My form looks like this:

    // Directions Form
       function DirectionsForm() {
    // Create fields
    $fields = new FieldSet(
    new TextField('Postcode', 'Enter Postcode')
    );
          
    // Create actions
    $actions = new FieldSet(
    new FormAction('doDirections', 'Submit')
    );
          
    $form = new Form($this, 'DirectionsForm', $fields, $actions);
          
          return $form;
    }
       
       
       function doDirections($data, $form) {
          Session::set('StartPoint', $data->$postcode);
          Director::redirect('get-directions/');
    }

    The template I wish to display my postcode on, then looks like this:

    function Postcode() {
          Session::get('StartPoint');
       }

    And finally, the action to display the value is:

    <% control Postcode %>
    <h2>My Postcode is: $data('StartPoint')   </h2>
    <% end_control %>

    I know I'm missing something fundamentally basic here, but my brain has blanked - can anyone point me in the right direction please?

  • novaweb
    Avatar
    Community Member
    109 Posts

    Re: Retrieving Session data Link to this post

    Hey Chilli-D,

    Multiple things are wrong here, hopefully this should work for you:

    Code:

    function DirectionsForm() {
    $fields = new FieldSet(
    new TextField('Postcode', 'Enter Postcode')
    );

    $actions = new FieldSet(
    new FormAction('doDirections', 'Submit')
    );

    $form = new Form($this, 'DirectionsForm', $fields, $actions);
    return $form;
    }

    function doDirections($data, $form) {
    Session::set('StartPoint', $data['Postcode']);
    Director::redirect('get-directions/');
    }

    function Postcode() {
    Session::get('StartPoint');
    }

    Template:

    <% control Postcode %>
       <h2>My Postcode is: $Postcode</h2>
    <% end_control %>

  • Chilli-D
    Avatar
    Community Member
    5 Posts

    Re: Retrieving Session data Link to this post

    Thanks novaweb!

    I worked this out late last night - however, you have been a little more efficient than me, so I will probably use this!

    Chilli

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