Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Help with simple form

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 862 Views
  • EricNZ
    Avatar
    Community Member
    1 Post

    Help with simple form Link to this post

    Hi, I am currently working on a website and am having trouble with forms.

    I am attempting to create a simple quiz where the user has one question with 5 options. I have got to the stage where the form is created, but I am stuck on how to return / pass the value they have selected. Following the tutorials and documentation, I have got this so far. Basically all this is trying to do is display weather the user has selected option 1 or not, however I am not that familiar with php and am struggling to pass the value.

    Any help or suggestions would be greatly appreciated.

    class Quiz_Controller extends Page_Controller {

    function Quiz() {
    // Create fields
    $fields = new FieldSet(
    // List the your fields here
    new OptionsetField(
    $name = "Quiz1",
    $title = "Test Qustion...",
    $source = array(
    "1" => "Option 1",
    "2" => "Option 2",
    "3" => "Option 3",
    "4" => "Option 4",
    "5" => "Option 5"
    )
    ));

    // Create actions
    $actions = new FieldSet(
    new FormAction('doQuiz', 'Submit')
    );

    return new Form($this, 'Quiz', $fields, $actions);
    }

    function doQuiz($source) {

    if ($source=="1")
    echo "Option1 selected";
    else
    echo "Option1 not selected";

    }

    }

    Again, it is this last function, doQuiz, or more so the passing of values between the two functions, that I am struggling with.

    Thankyou,
    Eric

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: Help with simple form Link to this post

    All your values are stored in the array $source ..

    Use $source['source'] to get the requested value.

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