![]() |
form_checkbox Checked/UnChecked - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: form_checkbox Checked/UnChecked (/showthread.php?tid=23887) |
form_checkbox Checked/UnChecked - El Forum - 10-25-2009 [eluser]PHP Programmer[/eluser] Hello Everyone, I am using a form having some dynamic checkboxes that are created by applying a loop over the result fetched from database. I am looking the way of handling these checkboxes that are set to checked or unchecked. More specifically, if I check a checkbox then its DB entry should be changed to '1' and if I uncheck it then entry should be '0'. I am using following code: <? foreach ($page['questions'] as $question) { ?> <tr> <?php if($question['active'] == '1') { $var = "1"; $type = TRUE; } else $var = "0"; $type = FALSE; ?> <?=form_checkbox("Q_{$question['questionid']}_active",$var,$type) ?> <? } ?> But it is not functioning as desired. It always sets '1'. Please suggest what is wrong... TIA Anuj form_checkbox Checked/UnChecked - El Forum - 10-25-2009 [eluser]imn.codeartist[/eluser] your problem is at your else condition Code: else should be Code: else form_checkbox Checked/UnChecked - El Forum - 10-25-2009 [eluser]PHP Programmer[/eluser] thanks dixcode for your fast reply....but it didn't work. It is again going for entry '1' only. I am not able to set it unchecked. form_checkbox Checked/UnChecked - El Forum - 10-25-2009 [eluser]imn.codeartist[/eluser] after the following code Code: <? foreach ($page[‘questions’] as $question) { ?> print your output by adding following line Code: echo $question['active'].'<br/>'; and check if your $question['active'] has any 0 values form_checkbox Checked/UnChecked - El Forum - 10-25-2009 [eluser]PHP Programmer[/eluser] I did it and it is displaying all '1', there was no '0'. But this is due to dynamic value set in the statement: <?=form_checkbox(“Q_{$question[‘questionid’]}_active”,$var,$type) ?> If I write the above statement as: <?=form_checkbox(“Q_{$question[‘questionid’]}_active”,0,$question['active']) ?> then it sets all checkbox entries to '0'. How can I set it dynamically....? form_checkbox Checked/UnChecked - El Forum - 10-25-2009 [eluser]PHP Programmer[/eluser] I noticed, if I write: <?=form_checkbox(“Q_{$question[‘questionid’]}_active”,1,$question[‘active’]) ?> and <?=form_checkbox(“Q_{$question[‘questionid’]}_active”,1,$question[‘active’]) ?> then the values in DB are being set '1' and '0' respectively. I also noticed that after setting '0' in the above statement, I am able to update DB table to '1' (by checking the checkboxes) but after setting '1' in that statement, DB table is not being updated to '0' again. Is there any problem in my update query? Please suggest.. Thanks |