Jump to content

PHP/MYSQL checkbox arrays


darshanpm

Recommended Posts

Hi Friends,

 

I have a form with list of checkboxes that are generated dynamically based on other table in mysql.  I save the selected checkbox values in mysql table as a string separated with , (1,2,3,4,5).  Now, i want people to edit the form again.  It will load the other values of the form and the checkboxes, but i want to have the checkboxes selected which were selected and saved in mysql.

 

Hope i am clear about what i am looking to do.  So, i want to retrieve the checkbox values (1,2,3,4,5) which i have stored earlier and then check against all the checkboxes and selecting them if the value exists in the array.  Can anyone help me to do this and provide me some sample code or tutorial link.

 

Thanks,

Darshan

Link to comment
https://forums.phpfreaks.com/topic/144437-phpmysql-checkbox-arrays/
Share on other sites

<?php
$options_from_db = '1,2,3,4,5';
$values = explode(',', $string);

$all_options = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

foreach ($all_options as $option) {
    echo '<input type="checkbox" name="values[]" value="' . $option . '" ' . ((in_array($option, $options_from_db)) ? 'checked="checked"' : '') . '/>';
}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.