0

I have a codeigniter form that contains two checkboxes:

<input type="checkbox" checked="checked" id="box1" name="box1" /> <label for="box1">Box One</label> 
<input type="checkbox" checked="checked" id="box2" name="box2" /> <label for="box2">Box Two</label>

I want to use CI form validation to check that at least one checkbox is select (more than one can be selected). I'm having problems working out how to do it with the form validation libaray. I tried a callback but this only passes the value of one checkbox. Whats the best way to validate?

There are also other fields on the form, so I need to maintain the state of the checkboxes on validation.

1 Answer 1

5

The easiest way would be to make the checkboxes into an array

<input type="checkbox" checked="checked" id="box1" name="checkboxes[]" /> <label for="box1">Box One</label> 
<input type="checkbox" checked="checked" id="box2" name="checkboxes[]" /> <label for="box2">Box Two</label>

and then check if the checkboxes array is set.

if ( isset( $_GET['checkboxes'] ) ) {}

Browsers don't send data for checkboxes if they are unchecked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.