Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
I don't see your PHP code in the question. – Jocelyn Apr 26 at 15:11
add comment (requires an account with 50 reputation)

4 Answers

up vote 0 down vote accepted

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.

share|improve this answer
Brillaint thankyou – snookian Apr 26 at 15:43
add comment (requires an account with 50 reputation)

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

share|improve this answer
add comment (requires an account with 50 reputation)

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"';?> />
share|improve this answer
add comment (requires an account with 50 reputation)

Add this code inside your input tag

<?php if ($tag_1 == 'yes') echo "checked='checked'"; ?>
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.