Welcome Guest, Not a member yet? Register   Sign In
form_checkbox Checked/UnChecked
#1

[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>

&lt;?php
if($question['active'] == '1') {
$var = "1";
$type = TRUE;
}
else
$var = "0";
$type = FALSE;
?&gt;
&lt;?=form_checkbox("Q_{$question['questionid']}_active",$var,$type) ?&gt;
&lt;?
}
?&gt;

But it is not functioning as desired. It always sets '1'.

Please suggest what is wrong...

TIA
Anuj
#2

[eluser]imn.codeartist[/eluser]
your problem is at your else condition
Code:
else
$var = “0”;
$type = FALSE;

should be

Code:
else
{
$var = “0”;
$type = FALSE;
}
#3

[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.
#4

[eluser]imn.codeartist[/eluser]
after the following code
Code:
&lt;? foreach ($page[‘questions’] as $question) { ?&gt;

print your output by adding following line

Code:
echo $question['active'].'<br/>';

and check if your $question['active'] has any 0 values
#5

[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:

&lt;?=form_checkbox(“Q_{$question[‘questionid’]}_active”,$var,$type) ?&gt;


If I write the above statement as:

&lt;?=form_checkbox(“Q_{$question[‘questionid’]}_active”,0,$question['active']) ?&gt;

then it sets all checkbox entries to '0'. How can I set it dynamically....?
#6

[eluser]PHP Programmer[/eluser]
I noticed, if I write:

&lt;?=form_checkbox(“Q_{$question[‘questionid’]}_active”,1,$question[‘active’]) ?&gt;
and
&lt;?=form_checkbox(“Q_{$question[‘questionid’]}_active”,1,$question[‘active’]) ?&gt;

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




Theme © iAndrew 2016 - Forum software by © MyBB