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 /

Accessing POST Parameters


Reply


5 Posts   3509 Views

Avatar
katja

Community Member, 46 Posts

11 March 2010 at 3:36am

Hi all,

I can't work out how to access POST parameters of a form (without previously writing them to the database or a session).

I would like to make the content of a page dependent on whether a checkbox has been ticked, but when after submitting the form I redirect to the same page, or another page, the parameters are lost.

Can somebody help? Thank you
Katja

Avatar
banal

Community Member, 901 Posts

11 March 2010 at 4:26am

Hi

POST parameters are only accessible when you actually receive POST params with your HTTP Request. This usually happens when you submit a Form via POST, but of course these parameters aren't being re-sent for subsequent pages.
If you want some sort of flag to be constantly set after you've received some POST parameters, you should either use the session or a cookie. Do not store sensitive information in the cookie though... well, actually you shouldn't store that in the session either, but it's the lesser of two evils.

Avatar
katja

Community Member, 46 Posts

11 March 2010 at 5:59am

Edited: 11/03/2010 8:00am

Thanks very much for your answer. Yes, for subsequent requests the parameters are gone, redirectBack() is already a subsequent request I suppose. Does that mean I cannot use SilverStripe forms in that way? I cannot process a form and show content depending on whether a checkbox in that form has been ticked? - Unless I first write it in a database, cookie or Session (Session::set stores the data in a cookie I think?).

What I wanted to do is ask people to tick a box, and only if that box is ticked they get redirected to the same page showing different content - or other page. But that page should not be accessible by directly calling the URL. Can you give me an idea how best to achieve that?

Thanks again
Katja

P.S. For a moment I was not sure anymore if one can even do it in just PHP, so I wrote this little example. So what I mean is, how could I get the behaviour this script displays, but using SilverStripe? I'm sure it must be possible, but I couldn't figure it out so far.

<?php

if(isset($_POST["accept"])) {
echo $_POST["accept"];} else {
?>

<form method="post" action="">
<input type="checkbox" name="accept" id="accept" />
<label for="accept">I accept the Terms and Conditions</label><br />
<input type="submit" value="Submit" title="Submit" />
</form>

<?php
}
?>

Avatar
banal

Community Member, 901 Posts

11 March 2010 at 9:50am

Hi katja

Ok, I see what you want to do. It should work actually, that's how forms are supposed to work, aren't they? How did you build your form?
Be sure to write a method on your controller that handles the form. Have a look at the following doc-pages for pointers:
http://doc.silverstripe.org/doku.php?id=tutorial:3-forms
http://doc.silverstripe.org/doku.php?id=recipes:forms
http://doc.silverstripe.org/doku.php?id=form

Oh and just to clarify the question regarding the session:
Sessions store data on the server, a cookie is being used to identify the session (only the session-id, but no actual data is stored in the cookie).
Cookies however store whatever data you add to it on the client computer.
Therefore a session is much safer, because you only transfer and store an identifier (session-id) instead of the sensitive data.

Avatar
katja

Community Member, 46 Posts

11 March 2010 at 10:15am

Edited: 11/03/2010 10:24am

Thank you very much, this is very helpful. I wasn't aware of the 2nd and 3rd document. - I did have a method to handle the form but somehing with it must have not been right. Sure will manage now. Also interesting about sessions and cookies, I must have read that before but had not taken it in properly.

Katja