0

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 Answers 3

4

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 '/>';
 ?>
Sign up to request clarification or add additional context in comments.

Comments

1

Just a 1-line shorter version ;)

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

Comments

0

Like this:

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

1 Comment

i would go with the solution provided by @shamittomar that sounds feasible, thank you for your take..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.