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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Retrieving Session data


Go to End


3 Posts   1668 Views

Avatar
Chilli-D

Community Member, 5 Posts

26 March 2012 at 11:07pm

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?

Avatar
novaweb

Community Member, 116 Posts

27 March 2012 at 2:32pm

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 %>

Avatar
Chilli-D

Community Member, 5 Posts

27 March 2012 at 9:36pm

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