tags:

views:

44

answers:

3

Hi,

I have a field in my update form called approve which is using the html checkbox element. now i am querying the approve value from the database which will hold the binary value (0 or 1), i want the checkbox to perofrm the following actions in condition.

a) While Querying from database.

1)if the value of active is 1 then it should be checked by default and also it should hold the value 1 to process it to update 2)the same applies for 0, if the value is zero then it is unchecked and will hold the value 0 to process

P.S: I want to use this for updating the form not inserting.

+3  A: 

Do it like this:

PHP embedded in HTML way:

 <input name="chk" type="checkbox" value="<?=$value?>" <?php if($value==1) echo 'checked="checked"';?> />

Pure PHP way:

 <?php
 echo '<input name="chk" type="checkbox" value="'.$value.'"';
 if($value == 1)
      echo ' checked="checked" ';
 echo '/>';
 ?>
shamittomar
Nice and thank you.. it is solved..
Ibrahim Azhar Armar
You're welcome.
shamittomar
A: 

Like this:

<input type="checkbox" value="<?php echo $row['approved']; ?>" <?php if($row['approved'] == 1): echo 'checked="checked"'; endif; />
krike
i would go with the solution provided by @shamittomar that sounds feasible, thank you for your take..
Ibrahim Azhar Armar
absolutely no problem :)
krike
+1  A: 

Just a 1-line shorter version ;)

<?php echo "<input name=\"chk\" type=\"checkbox\" value=\"$value\"".( ($value == 1) ? " checked=\"checked\"" : "" )." />"; ?>
migajek