Jump to content

Set checkbox values of form according to session keys


Recommended Posts

Hello, I have a custom form featuring 9 checkboxes. The user makes their selections and submits to set a session called "itinerary" with key (foodie) and value (1) for each selection. How can I populate those checkboxes based on the "itinerary" session when the form is viewed again. I've referenced Skyscrapers, but wasn't able to apply it here. Also, would I need to sanitize user input for a checkbox? If so, would I do so in the loop before whitelisting? ($value = $sanitizer->selectorValue($value))

    if(isset($_GET['submit'])) {
        // Remove existing "itinerary" sessions
        // Loop through all inputs to create an associative array for storing selected checkboxes in session
        foreach($input->get() as $key => $value) {
        	$value = (int) $value; 
        	$input->whitelist($key, $value);
        	$itinerary[$key] = $value;

        $session->set("itinerary", $itinerary);        
    echo "<pre>".var_dump($session->itinerary)."</pre>";


array(4) { ["foodie"]=> int(1) ["love-birds"]=> int(1) ["family"]=> int(1) ["submit"]=> int(1) }


Link to comment
Share on other sites

I placed this within each input of the form based on the name of the field and it works.

if(is_array($session->itinerary) && array_key_exists('foodie', $session->itinerary)) echo 'checked';

Still not clear about sanitizing the input from the initial form submittal though.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...