A custom block with multiple checkboxes
Permalink
I'm new to concrete5 and i wanted to know how to retrieve value of multiple checkboxes having the same name!
As in , if all are checked store and retrieve all three values
As in , if all are checked store and retrieve all three values
<input type="checkbox" name="test[]" value="1"/> <input type="checkbox" name="test[]" value="2"/> <input type="checkbox" name="test[]" value="3"/>
Could you please help me out by tellin me what my controller save() should be?
That all depends on what your database table looks like. Can you post the relevant declarations of the db.xml.
<?xml version="1.0"?> <schema version="0.3"> <table name="simpleTest"> <field name="bID" type="I"> <key ></key> <unsigned ></unsigned> </field> <field name="test" type="X2"> </field> </table> </schema>
Here is the sort of code I tend to use in a save() method.
If all your test[] checkboxes were checked, it would save the string "1,2,3"
When using in edit/view, you can then do something like:
(I obviously need to communicate between controller and view somewhere in the middle of that)
Depending on the options, I have also used a similar trick with serialize/unserialize and to/from json.
public function save ($data) { if (is_array($data['test'])){ $data['test'] = implode(',',array_values($data['test'])); }else{ $data['test'] = implode(',',array()); } parent::save($data); }
If all your test[] checkboxes were checked, it would save the string "1,2,3"
When using in edit/view, you can then do something like:
(I obviously need to communicate between controller and view somewhere in the middle of that)
Depending on the options, I have also used a similar trick with serialize/unserialize and to/from json.
Thank you!! It worked well :)
e.g.
For general debugging, see
http://www.concrete5.org/documentation/how-tos/developers/concrete5...
You can see what parameters c5 sees by doing a dump of $_POST.