Reputation: 51
I've made dynamic forms that can update an entire list of options with a single submit button, using the foreach($_POST)
technique, but for some reason, I am having issues achieving the same thing with checkbox inputs.
No matter which checkboxes I deselect, the script that evaluates the checkboxes and treats the final rows in the form as the ones I deselected. In other words, no matter which checkbox I deselect, the POST script will determine the final checkbox was deselected. It's a very strange effect I've never seen before.
For example, this PHP array fetch generates rows of inventory items with the option to hide the item.
****Note that I have not put in the security measures yet. When I see this work logically I will sanitize everything. I don't even want to use REQUEST at the moment, but I want to see this work first.
<form action="update.php" method="post">
while($row=mysql_fetch_array($histresult))
{
echo '<tr height = "50px">';
//We grab the product id as well as everything else we need.
$customer_id= $row['customer_id'];
$product_id= $row['product_id'];
$link = $row['image_path'];
$name = $row['name'];
$sku_item_number = $row['sku_item_number'];
$suggested_quantity = $row['suggested_quantity'];
$sales_info = $row['sales_info'];
$item_id = $row['id_product_customer_suggested_qty'];
$customer_level = $row['level'];
//Visibility Option Goes here
echo '<td>';
echo '<input name="i" type="hidden" value="'.$i.'">
<input name="product_id[]" type="hidden" value="'.$product_id.'">
<input name="customer_id[]" type="hidden" value="'.$customer_view.'">
<input name="customer_level[]" type="hidden" value="'.$customer_level.'">';
echo '<input name="cart_visible[]" type="checkbox" value = "1"';
if (!in_array($product_id, $hidden))
{
echo 'checked = "checked"';
}
echo '></td>';
echo '<td>';
echo '<a href="displayitem.php?product_id='.$product_id.'">'.$name.'</a>';
echo '</td>';
echo '<td>'.$sku_item_number.'</td>';
echo '<td>'.$product_id.'</td>';
echo '<td>'.$sales_info.'</td>';
echo '<td>'.$suggested_quantity.'</td>';
echo '</tr>';
<input type="submit" name="button" id="button" value="Submit">
</form>
//And then update.php looks like this:
$j=0;
foreach($_REQUEST['product_id'] as $key=>$product_id)
{
echo 'j value is '.$j.'<br>';
$customer_id = $_REQUEST['customer_id'][$key];
$customer_level = $_REQUEST['customer_level'][$key];
$price_level_id = $_REQUEST['price_level_id'][$key];
$cart_visible = $_REQUEST['cart_visible'][$key];
if ($cart_visible != 1)
{
$cart_visible = 0;
}
//echo 'Customer ID is '.$customer_id.'<br>';
echo 'Testing - Product ID is '.$product_id.'<br>';
//echo 'Customer Level is '.$customer_level.'<br>';
//echo 'Price Level ID is '.$price_level_id.'<br>';
echo 'Cart visibility selection is '.$cart_visible.'<br>';
}
The echoing debug lines reveal that no matter which rows I deselect, it treats the final rows in the form as the ones that are deselected. For example, if there are ten rows and I deselect the checkboxes of rows 1, 3, and 8, the form will act like the user deselected the 8th, 9th and 10th checkbox. Why would it do this exclusively for checkboxes?
Upvotes: 0
Views: 739
Reputation: 5619
When you post a checkbox that was deselected, it does not send its "value", hence is not in POST.
If the checkbox was part of an array name, and there were ten checkbox's, 3 deselected, the resulting array in POST will simply have 7 indexs 0 to 6.
to solve this problem, give the checkbox names a value for the array..
I may have not properly understood your code, but something like this
$ite=0;
while($row=mysql_fetch_array($histresult)){
//....
echo '<input name="cart_visible['.$ite.']" type="checkbox" value = "1"';
//....
$ite++;
}
Now, say you deselected checkbox number 3, which means the 3rd index will not be there. $cart_visible part of code should look something like this
//if checkbox value exists, make $cart_visible to that value, else make $cart_visible to 0
$cart_visible = isset($_REQUEST['cart_visible'][$key])?$_REQUEST['cart_visible'][$key]:0;
and remove the if statement for $cart_visible
Upvotes: 2