PDA

View Full Version : PHP How to deal with un-checked checkboxes?



bernk
December 9th, 2008, 08:57 AM
Hi guys. I could really use some help with this problem.

I'm building a little CMS to manage a basic product catalog. Each product's information is stored in a products table in a MySQL DB. The parts I'm concerned about here are the product's unique ID (primary key) and the product's availability (boolean). When added a product is available by default (1). In the CMS the products are displayed in a simple table with each row having a checkbox which reflect the current state of that product's availability:

1 x
2 x
4 x
6 x
9 x



// this is how the actual inputs look like
<input type='checkbox' name='available[]' value='1' checked />
<input type='checkbox' name='available[]' value='2' checked />
<input type='checkbox' name='available[]' value='4' checked />
<input type='checkbox' name='available[]' value='6' checked />
<input type='checkbox' name='available[]' value='9' checked />


Now let's say I uncheck one of the rows in the table:

1 x
2 x
4
6 x
9 x

How can I get PHP to update the MySQL table with the unchecked row(s)? I have been successful in accomplishing the opposite, but am having trouble with writing a loop for the unchecked IDs. Please note that the IDs are not sequential since products can be deleted, which leaves holes.

My code for updating freshly checked boxes looks like this:



if ($_GET['action']=="update")
{
require("includes/connect.ini");
foreach ($_POST['available'] as $id)
{
$query = sprintf("UPDATE products SET available='%u' WHERE id='%u'", 1, mysql_real_escape_string($id));
$result = mysql_query($query, $con);
if (!$result)
{
echo 'could not update availability of products: ' . mysql_error($con);
exit;
}
}
mysql_close($con);
}


This would be a lot easier if the checkbox array would contain unchecked boxes with a value of 0, but it does not. It only contains the checked boxes.

simplistik
December 9th, 2008, 03:03 PM
you'll have to give them unique names and then check against those unique names so instead of


<input type='checkbox' name='available[]' value='1' checked />
<input type='checkbox' name='available[]' value='2' checked />
<input type='checkbox' name='available[]' value='4' checked />
<input type='checkbox' name='available[]' value='6' checked />
<input type='checkbox' name='available[]' value='9' checked />

you'll need to do something like:


<input type='checkbox' name='available[1]' value='1' checked='checked' />
<input type='checkbox' name='available[2]' value='2' />
<input type='checkbox' name='available[3]' value='4' checked='checked' />
<input type='checkbox' name='available[4]' value='6' checked='checked' />
<input type='checkbox' name='available[5]' value='9' checked='checked' />

then in your post you'll have to check for those individually in your post


echo $_POST['available'][2] ? 'foo' : 'bar';

if you're using PHP to draw the checkboxes out in the first place you should be able to use a similar method to check for all the $_POST instances