Page 1 of 1

Populate form checkboxes on a edit page.

Posted: Wed Aug 11, 2010 11:12 am
by Brad_Strickland
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

Re: Populate form checkboxes on a edit page.

Posted: Wed Aug 11, 2010 12:53 pm
by Gargoyle
in order to have checkboxes checked by default, you need to add "checked" in the html code:

Code: Select all

<input type=checkbox checked>

Re: Populate form checkboxes on a edit page.

Posted: Wed Aug 11, 2010 1:48 pm
by Brad_Strickland
I know that. What is need is on the edit page have the check boxes checked whose values are in the database.
So the script would look at what was in the database and automatically check the appropriate checkboxes.