PHP checkbox set to check based on database value
Asked Answered
B

6

17

I have a system where people fill in their information and later can go back and edit certain parts, basically the enter personal info and check whether they want to know extra info, these bits of extra infos are checkboxes, 4 of them. the user will select up to any of the 4 and the database has 4 fields set to no, if they select one it changes to yes. I want them to be able to go back and deselect or reselect any of these 4 checkboxes, so what i want s for the checkboxes to be selected if the values is yes and unselected if the value is not.

the fields are tag_1, tag_2, tag_3, tag_4

Anyhelp greatly appreciated

I gather some kind of if statement but not sure how to involve it in a checkbox.

<label for="tag_1">Tag 1</label>
<input type="checkbox" name="tag_1" id="tag_1" value="yes" />

Ian

Blatherskite answered 26/4, 2013 at 15:7 Comment(1)
I don't see your PHP code in the question.Oilcup
E
14

Add this code inside your input tag

<?php if ($tag_1 == 'yes') echo "checked='checked'"; ?>
Evildoer answered 26/4, 2013 at 15:12 Comment(0)
B
64

Extract the information from the database for the checkbox fields. Next change the above example line to: (this code assumes that you've retrieved the information for the user into an associative array called dbvalue and the DB field names match those on the HTML form)

<input type="checkbox" name="tag_1" id="tag_1" value="yes" <?php echo ($dbvalue['tag_1']==1 ? 'checked' : '');?>>

If you're looking for the code to do everything for you, you've come to the wrong place.

Brauer answered 26/4, 2013 at 15:12 Comment(0)
E
14

Add this code inside your input tag

<?php if ($tag_1 == 'yes') echo "checked='checked'"; ?>
Evildoer answered 26/4, 2013 at 15:12 Comment(0)
E
3

This simplest ways is to add the "checked attribute.

<label for="tag_1">Tag 1</label>
<input type="checkbox" name="tag_1" id="tag_1" value="yes" 
    <?php if($tag_1_saved_value === 'yes') echo 'checked="checked"';?> />
Elderberry answered 26/4, 2013 at 15:12 Comment(1)
Hey so I will update my question to reflect your answer which is exactly right you understand what I am trying to do here but now it it showing the correct available categories but if I check them now it does not post into the join table and of course not showing ticks because there are no rows?Trews
F
1

Use checked="checked" attribute if you want your checkbox to be checked.

Firer answered 26/4, 2013 at 15:12 Comment(1)
I realize this is old but I appreciate a "no-nonsense" kind of answer.Sculpin
P
1

You can read database value in to a variable and then set the variable as follows

$app_container->assign('checked_flag', $db_data=='0'  ? '' : 'checked');

And in html you can just use the checked_flag variable as follows

<input type="checkbox" id="chk_test" name="chk_test" value="1" {checked_flag}>
Perni answered 24/9, 2014 at 21:49 Comment(0)
K
0
$Active=($row['Active']==1)?'checked':"";
<input class='w3-check' type='checkbox' $Active>
Kolk answered 26/4, 2024 at 6:40 Comment(1)
Welcome to StackOverflow. Please, edit and try for How to Answer, describe the effect of what you propose and explain why it helps to solve the problem, the relevant advantage which your solution achieves. Find help with formatting your post here: stackoverflow.com/help/formatting . Consider taking the tour.Overjoy

© 2022 - 2025 — McMap. All rights reserved.