Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Accessing POST Parameters

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

Page: 1
Go to End
Author Topic: 3363 Views
  • katja
    Avatar
    Community Member
    46 Posts

    Accessing POST Parameters Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Accessing POST Parameters Link to this post

    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.

  • katja
    Avatar
    Community Member
    46 Posts

    Re: Accessing POST Parameters Link to this post

    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
    }
    ?>

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Accessing POST Parameters Link to this post

    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.

  • katja
    Avatar
    Community Member
    46 Posts

    Re: Accessing POST Parameters Link to this post

    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

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