Razvan Cuceu
Razvan Cuceu

Reputation: 577

Check checkboxes from mysql db

I have a form that submits a checkboxes form to db in the same column from db like "Windows,Android,iOS". And I have a "edit" page that has to show those checkboxes again, but those checkboxes taken from db to be checked. In this case if i have the previous string taken from db i should get like this(bold is checked):

I know how to get an array from the string taken from the db with

$devices = "Windows,Android,iOS";
$arrDevices = explode(',', $devices);

But I don't know the algorithm to check the corresponding checkboxes in here

<input type="checkbox" name="platform" value="Windows">Windows
<input type="checkbox" name="platform" value="OS X">OS X
<input type="checkbox" name="platform" value="Android">Android
<input type="checkbox" name="platform" value="iOS">iOS
<input type="checkbox" name="platform" value="Windows Phone">Windows Phone

Upvotes: 1

Views: 51

Answers (1)

George Kagan
George Kagan

Reputation: 6124

Add this to the end of each input (changing the values, of course):

<?php print in_array('Windows', $arrDevices) ? 'checked' : '' ?>

Upvotes: 1

Related Questions