• Hey guys,

    I have a list of checkboxes and am wanting to build in some kind of loop so I don’t have to list every single checkbox. Currently I just have 2 to test with:

    // Sanitize and validate input. Accepts an array, return a sanitized array.
    	function lc_options_validate($input) {
    		// Our first value is either 0 or 1
    		$input['option1'] = ( isset($input['option1']) == 1 ? 1 : 0 );
    		$input['option2'] = ( isset($input['option2']) == 1 ? 1 : 0 );
    
    		return $input;
    	}

    I am drawing a blank, any ideas on how I can have this loop?
    Thanks!

Viewing 1 replies (of 1 total)
  • Hi

    If you were to build the checkboxes like this, PHP would read them as an array ($_POST['my-checkbox']) where only the checked elements would exist and have a value of “1”:

    <label for="my-checkbox-1"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-1" /> Checkbox 1</label>
    <label for="my-checkbox-2"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-2" /> Checkbox 2</label>
    <label for="my-checkbox-3"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-3" /> Checkbox 3</label>
    <label for="my-checkbox-4"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-4" /> Checkbox 4</label>

Viewing 1 replies (of 1 total)
  • The topic ‘Checkbox validation’ is closed to new replies.