Welcome Guest, Not a member yet? Register   Sign In
Handling Multiple Checkboxes
#1

[eluser]Unknown[/eluser]
Hello CI community ---

I'm pretty new to CI and I'm trying to figure out the best way to persist multiple checkbox values to the DB. I searched the forums for a bit but did find anything I was looking for.

Here is what I have so far..In the DB I have a column set up for each checkbox with the goal of persisting either a 1 or 0 depending if the checkboxes is checked.

Code:
&lt;input type="checkbox" name="day[]" value="1" id="monday"  /&gt;&lt;label for="monday">&nbsp;Monday</label><br />
&lt;input type="checkbox" name="day[]" value="1" id="tuesday"  /&gt;&lt;label for="tuesday">&nbsp;Tuesday</label><br />
&lt;input type="checkbox" name="day[]" value="1" id="wednesday"  /&gt;&lt;label for="wednesday">&nbsp;Wednesday</label><br />
&lt;input type="checkbox" name="day[]" value="1" id="thursday"  /&gt;&lt;label for="thursday">&nbsp;Thursday</label><br />
&lt;input type="checkbox" name="day[]" value="1" id="friday"  /&gt;&lt;label for="friday">&nbsp;Friday</label><br />

Most of the checkboxes are grouped so I have named them the same. My issue, as I understand it, is sometimes not all the checkboxes are checked every time the form is submitted so they won't show up in the POST. Which then leads me to my next problem of mapping each value in the POST to my DB table. What is the most effective way to handle this issue?

Thanks,

Ryan
#2

[eluser]Noahs Arkive[/eluser]
I think what you want is something like:

&lt;input type="checkbox" name="day[]" value="monday" id="monday" /&gt;&lt;label for="monday">&nbsp;Monday</label><br />
&lt;input type="checkbox" name="day" value="tuesday" id="tuesday" /&gt;&lt;label for="tuesday">&nbsp;Tuesday</label><br />
&lt;input type="checkbox" name="day" value="wednesday" id="wednesday" /&gt;&lt;label for="wednesday">&nbsp;Wednesday</label><br />
&lt;input type="checkbox" name="day" value="thursday" id="thursday" /&gt;&lt;label for="thursday">&nbsp;Thursday</label><br />
&lt;input type="checkbox" name="day" value="friday" id="friday" /&gt;&lt;label for="friday">&nbsp;Friday</label><br />

If monday and wednesday are checked (no need for an array, 'day' will return a comma-delimeted list or does not exist):

// Use the Input class; it's automatically loaded by System
$cked=$this->input->post("day");
echo $cked; // 'monday,wednesday' or FALSE is nothing is clicked
#3

[eluser]Craig A Rodway[/eluser]
Usually you would want to normalise that data and create another table to store the days - but you'd also need to change the values for each day - at the moment, day[] is probably being set to '1' no matter which ones are checked. Once you use values from 1 through to 5, you will end up with an array called day[] with values being set properly.

If you don't need to search the database using the days, you could use the PHP serialize() function to convert the day[] array to a string which you can store in a single DB field.
#4

[eluser]Unknown[/eluser]
I'm not sure that solves the issue because the post is still stored within one variable -- need a way to parse out the values separately.

Ryan
#5

[eluser]Craig A Rodway[/eluser]
HTML code:

Code:
&lt;input type="checkbox" name="day[]" value="1" id="monday"  /&gt;...
&lt;input type="checkbox" name="day[]" value="2" id="tuesday"  /&gt;...
&lt;input type="checkbox" name="day[]" value="3" id="wednesday"  /&gt;...
&lt;input type="checkbox" name="day[]" value="4" id="thursday"  /&gt;...
&lt;input type="checkbox" name="day[]" value="5" id="friday"  /&gt;...

If you tick Monday, Wednesday and Friday; you will get an array ($days = $this->input->post('day')) which has 3 elements - the values 1, 3 and 5. Then you can do whatever you want with the array - implode(), serialize(), whatever...




Theme © iAndrew 2016 - Forum software by © MyBB