Populate form checkboxes on a edit page.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brad_Strickland
Forum Newbie
Posts: 3
Joined: Tue Jul 13, 2004 2:08 pm
Contact:

Populate form checkboxes on a edit page.

Post 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
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: Populate form checkboxes on a edit page.

Post 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>
Brad_Strickland
Forum Newbie
Posts: 3
Joined: Tue Jul 13, 2004 2:08 pm
Contact:

Re: Populate form checkboxes on a edit page.

Post 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.
Post Reply