tags:

views:

308

answers:

4

I have an array of checkboxes that I edit at once to set up a 'tinyint' field. the problem comes in when i uncheck the checkbox and post the vales to mysql. since it posts an array of checkboxes and another parallel array of values to edit, unchecking a checkbox results in the 0 value been ignored by PHP_POST and hence the checkbox array will be less by the number of unchecked values in the form while the array to be edited will have all the records in the form.

here is the submit code

while($row=mysql_fetch_array($result))
{

$checked = ($row[active]==1) ? 'checked="checked"' : '';
...

echo "<input type='hidden' name='TrID[]' value='$TrID'>";
echo "<input type='checkbox' name='active1[]' value='$row[active]''$checked' >";
...

and the processing php script

$userid = ($_POST['TrID']);
$checked= ($_POST['active']);

$i=0;

foreach ($userid as $usid) 
{

if ($checked[$i]==1){
$check = 1;
}
else{
$check = 0;
}

$qry1 ="UPDATE  `epapers`.`clientelle` SET  `active` =  '$check' WHERE  `clientelle`.`user_id` =  '$usid' ";
$result = mysql_query($qry1);   
$i++;

}
A: 

You can use the key of the first array to fetch the correct element of the second...

$userid = ($_POST['TrID']);
$checked= ($_POST['active']);

foreach ($userid as $key => $usid) 
{

if ($checked[$key]==1){
$check = 1;
}
else{
$check = 0;
}

$qry1 ="UPDATE  `epapers`.`clientelle` SET  `active` =  '$check' WHERE  `clientelle`.`user_id` =  '$usid' ";
$result = mysql_query($qry1);   

}

EDIT If this doesn't work, try to add numeric indexes to the element names in the HTML code. Like this:

echo "<input type='hidden' name='TrID[".$i."]' value='$TrID'>";
echo "<input type='checkbox' name='active1[".$i++."]' value='$row[active]''$checked' >";

You'd obviously have to set $i to zero before the loop.

Franz
A: 

You could try something like:

$i = 0;
while($row=mysql_fetch_array($result))
{

$i++;
$checked = ($row[active]==1) ? 'checked="checked"' : '';
...

echo "<input type='hidden' name='TrID[{$i}]' value='$TrID'>";
echo "<input type='checkbox' name='active1[{$i}]' value='$row[active]''$checked' >";
...

The checkboxes should then at least be in the proper place in the array...

Chibu
This shouldn't work. You'd still have less elements in the second array than in the first one.
Franz
it works since `if ($checked[$i]==1){` increments from 0 while POST posts $checked as `$checked[$i]`, when `$checked[$i]` is not posted when it is 0 , `$check` becomes 0 hence the code works!!
Doesn't that mean that you can get an "undefined index" error depending on the `error_reporting` setting?
Franz
I suppose it's possible. But, PHP is generally pretty indifferent to undefined variables. If you're concerned about it, you could always use something like `if (isset($checked[$i])) { $check = 1; }` or if you're even more worried than that: `try { if ($checked[$i] == 1){ $check = 1; } } catch($err) { $check = 0; }`
Chibu
A: 

Checkboxes shouldn't have the same name, but each one should be named differently. You could solve your problem by modifying the first page like this:

$i = 0;
while($row=mysql_fetch_array($result))
{

$checked = ($row[active]==1) ? 'checked="checked"' : '';
...

echo "<input type='hidden' name='TrID[$i]' value='$TrID'>";
echo "<input type='checkbox' name='active1[$i]' value='$row[active]''$checked' >";
$i++;
...

And accordingly adjust the second script...

Kjir
Assigning the elements to an array does give them different names.
Franz
In PHP but not in HTML
Kjir
A: 

I have my own script I use to accomplish this... see if you can adapt it or if I'm anywhere on track with what you're trying to do.

Schema: Rows of products each with a checkbox that User can check in order to signify that that product needs to be added to the cart in order to see the price.

Code:
<td><input type="checkbox" name="addforprice[<?php echo $record['id']; ?>]" /></td>

Then when the form is submitted:

if($_POST['addforprice']){
foreach(array_keys($_POST['addforprice']) as $var){
$data['add_for_price'] = 1;
$update = $db->query_update(TABLE_PRODUCTS, $data, "id=".$var);
echo "addforprice:".$var.' '.$data['add_for_price'].'
';
unset($data['addforprice']);
}
}

$record['id'] is the database id generated when printing out the rows.

bradenkeith