Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

A Simple HTML Checkbox with PHP Problem

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.

Upvotes: 0

Views: 395

Answers (3)

Christophe
Christophe

Reputation: 4828

Like this:

<input type="checkbox" value="<?php echo $row['approved']; ?>" <?php if($row['approved'] == 1): echo 'checked="checked"'; endif; />

Upvotes: 0

migajek
migajek

Reputation: 8614

Just a 1-line shorter version ;)

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

Upvotes: 1

shamittomar
shamittomar

Reputation: 46692

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 '/>';
 ?>

Upvotes: 4

Related Questions