Populate form checkboxes on a edit page.
Posted: Wed Aug 11, 2010 11:12 am
Here is my problem. I'm using the two below blocks of code to generate an array of checkboxes and then take the checked values and dump them in a database record. My problem is on the edit page I need to have the stored values checked in the form so that the user can edit them. How do I do this?
Code: Select all
//this generates the checkboxes
<?PHP
$connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
$db=mysql_select_db ("database", $connection) or die (mysql_error());
$query = "SELECT type FROM typelist ORDER BY type ASC";
$sql_result = mysql_query($query, $connection) or die (mysql_error());
$i=1;
echo '<table valign="top"><tr><td>';
while ($row = mysql_fetch_array($sql_result)) {
$type = $row["type"];
if ($i > 1 && $i % 26 == 0)
echo '</td><td>';
else if ($i)
echo '';
++$i;
echo "<input style='font-size:10px;' name='type[]' type='checkbox' value='$type'><span style='color:#000;'>$type</span></input><br/>";
}
echo '</td></tr></table>';
?>Code: Select all
// dumping the type of job checkboxes
// This block of code takes the check box values, comma separates them and put them in the database record
$allTypes = $_POST['allTypes'];
var_dump($allTypes);
// Setting up a blank variable to be used in the coming loop.
$allStyles = "";
// For every checkbox value sent to the form.
foreach ($type as $style) {
// Append the string with the current array element, and then add a comma and a space at the end.
$allTypes .= $style . ", ";
}
// Delete the last two characters from the string.
$allTypes = substr($allTypes, 0, -2);
// end dumping the type of job checkboxes