Jump to content

checkbox help


dennismonsewicz

Recommended Posts

I have this code:

 

<?php
if($chkbx) {
	foreach($chkbx as $key=>$val) 
	{
		$newval = $val==1?1:0;
	  $data_name .= $key . "=" . $newval . ",";								
	}

	$trimmedvar = trim($data_name, ",");

$qry = "UPDATE has_had_projects SET project = '$project_name', $trimmedvar WHERE project_id = '$id'";
echo $qry;
} ?>		

 

This works as far as updating the checkboxes that have been checked but what I can't get working is setting all of the other values to 0 if they are unchecked... any help here?

Link to comment
Share on other sites

the $chkbx var is set like this

 

$chkbx = $_POST['checkboxes'];

 

The code that is spitting out the checkboxes is this:

 

checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
$field = mysql_num_fields($checkbox_qry);
while($row = mysql_fetch_assoc($checkbox_qry)) {													
for($i = 2; $i < $field; $i++) {
$names = mysql_field_name($checkbox_qry, $i);	
$chk = $row[$names]==1?'checked="checked"':'';
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																	
$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1"  /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>';
}
echo $title;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.