black_belt
black_belt

Reputation: 6799

How to Populate Checkbox?

I have two fields in my database and they are- name and user_id. I have a page called name.php where I have fives names and checkboxes for each of those names. Now what I want to do is when I visit the page “name.php”, I want names checked which are already inserted in the database. Would you please kindly help me to do this? Thanks in Advance

Upvotes: 0

Views: 182

Answers (2)

Madara's Ghost
Madara's Ghost

Reputation: 174977

Go over the records of the database, and when generating the chekboxes, see if the name appears in the database. If it does, add checked="checked" to the checkbox.

Working Example

Upvotes: 0

Ibu
Ibu

Reputation: 43820

if the value in the db says it is checked you can add the checked attribute to the input element

<input type="checkbox" name="thename" checked="checked" />

so in your code it will be like:

<input type="checkbox" name="thename" <?php echo $value == 1 ? 'checked="checked"' : ''?> />

this will add the checked attribute if the value is equal to 1

Upvotes: 4

Related Questions