Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Checkbox from Model
  • Hello.

    I´m working in my new app, were i have a from for user registration, and there is a checkbox set to let the user choise interests.

    The interests categories comes from a Model, so i can addd/remove them from Admin panel.

    The question is, how i generate a checkbox for each item retrieved from the model? That, for a new user. In the registration proccess how should i store the selected values in the database? 

    And third, how do i retrieve that selected values (I.e. in the case of editing a profile) and show the checkboxed selected depending on the data retrieved from the current user?

    Sorry for my bad english, and sorry maybe it is a newbie question but i can not figure it out.... and today is the second day working on this :S
  • Assuming you use a Fieldset, you add the checkbox values in the 'options' array of the field, as key-value pairs ( like array('1' => 'option 1', '2' => 'option 2') ). It will be posted as an array of values, with the name of the field, and containing the boxes checked.
  • Actually, i'm working w/o fieldset, just using form and validation class. I really have problems to get it to work :S
  • HarroHarro
    Accepted Answer
    Form::checkbox() only creates a single checkbox, so the options thing does not apply.

    There's nothing difficult about it, use
    echo Form::checkbox('gender', 'Male', true);
    or
    echo Form::checkbox('gender', 'Male', 'Male');
    To display the checkbox checked. The third parameter must be either 'true', or match the value, to check the checkbox.

    If you have multiple checkboxes and you want the result in an array, just use array notation for the fieldname:
    echo Form::checkbox('gender[]', 'Male', true);
    or
    echo Form::checkbox('gender[12]', 'Male', true);
    if you want to use record id's as the index.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion