Reza Hatami
Reza Hatami

Reputation: 21

Show true or false (0/1) from a mySQL in checkbox

<input type="text" id="A1" name="A1">

In the box above, 0 or 1 is placed from mysql (fetch in box). Now I want that:

value box = 0 >> checkbox is unchecked

value box = 1 >> checkbox is checked

How to use a checkbox (type="checkbox") instead of text (type="text")?

<input type="checkbox" id="A1" name="A1">

Upvotes: 1

Views: 1744

Answers (5)

Mayur Vora
Mayur Vora

Reputation: 962

Hello Reza Hatami,

Try this below code,
$my_checkbox variable inside store data retrive from mysql data base.

<?php
    $my_checkbox = 1;
?>
<input type="checkbox" id="Notification" name="Notification" <?php echo if($mycheckbox == 1) ? 'checked=""': ""; ?> />

also using, But in this some problem is some browser or php version not support so my advise use above code.

<?php
   $my_checkbox = 1;
?>
    <input type="checkbox" id="Notification" name="Notification" <?=($my_checkbox == 1) ? "checked" : "" ?> />

I hope my answer is helpfull. If any query so comment please.

Upvotes: 0

Muhammad Muazzam
Muhammad Muazzam

Reputation: 2800

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

$value is from database

Upvotes: 0

Vladlen Pleshenkov
Vladlen Pleshenkov

Reputation: 69

You can use js

document.getElementById("A1").checked = true;

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

You can check do it like this:

<?php
$box_val = 1; // rertive this value from database
?>
<input type="checkbox" id="A1" name="A1" <?=($box_val == 1) ? "checked" : "" ?>>

Upvotes: 3

Alexander Higgins
Alexander Higgins

Reputation: 6923

You can do this:

<input id="A1" name="A1" checked="0" type="checkbox">
<script>
    $('#A1').prop("checked",$mySqlValue);
</script>

If $mySqlValue==1 the box will be checked and if $mySqlValue==0 it will not be checked.

Upvotes: 1

Related Questions