Jump to content

php/mysql store and retrieve checkbox values


jarvis

Recommended Posts

You can store 1 for a checked box and 0 for an unchecked box. So..

 

Inserting:

 

$checkbox = (isset($_POST['some-check-box'])) ? 1 : 0;
//Insert $checkbox into mysql..

 

Getting back from the database:

 

//Query etc...
$checkbox = ($row['checkbox'] == 0) ? NULL : 'checked="checked"';
echo "<input type='checkbox' name='whatever' $checkbox />";

For that it's a bit different, you'd store the selected value and do something like this:

 

Storing:

$selected = mysql_real_escape_string($_POST['select-menu']);
//Query

 

Then for displaying it with the selected one:

 

//Query..
$items = Array('Menu Option 1', 'Menu Option 2', 'Menu Option 3');
foreach($items as $item)
{
     $menu .= ($row['selected'] == $item) ? "<option value='$item' selected='selected'>$item</option>" : "<option value='$item'>$item</option>";
}

 

Then $menu would contain the contents of the <select..></select>.

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.