I'm stuck here. I'm basically have a set up where a user can dynamically add a new "item" row and inside each row is a checkbox. When the checkbox is checked, the value in the DB should be stored as 1 and if not, it is automatically stored as 0 (I have written up all this code).
However, I've got a much more fundamental problem: when a checkbox isn't checked, nothing is posted and thus my array's size is not what I expect it to be. Below, is some sample code which highlights my problem:
<form action="" method="post" target="_blank">
Enter name for item 1 <input type="text" name="item_name[]" required> <input type="checkbox" name="item_checked[]" value="yes"> <br />
Enter name for item 2 <input type="text" name="item_name[]" required> <input type="checkbox" name="item_checked[]" value="yes"> <br />
Enter name for item 3 <input type="text" name="item_name[]" required> <input type="checkbox" name="item_checked[]" value="yes"> <br />
<input type="submit" value="Submit">
</form>
<?php
for($i = 0; $i < count($_POST['item_name']); $i++){
$itemName = $_POST['item_name'][$i];
$itemChecked = "no";
if(isset($_POST['item_checked'][$i]) && $_POST['item_checked'][$i] == "yes"){
$itemChecked = "yes";
}
echo "item_name: $itemName, i: $i, itemChecked $itemChecked <br />";
}
?>
Simply speaking, the item_name textboxes are mandatory. The checkboxes (obviously) aren't. What I would like for to happen is if I check only the second checkbox, then this is registered in the POST data (but currently, when nothing is checked, nothing is posted so the value of $i is different and this it is set for the first item).
EDIT: I've added my entire (simplified) version of the code including the functionality where I add and remove rows, so you can better understand the issue:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".addCF").click(function(){
$("#products").append('<tr><td>Enter name for item <input type="text" name="item_name[]"> <input type="checkbox" name="item_checked[]" value="yes"> <a href="javascript:void(0);" class="remCF tip" title="Remove this item">Remove</a></td></tr>');
});
$("#products").on('click','.remCF',function(){
$(this).parent().parent().remove();
});
});
</script>
<a href="javascript:void(0);" class="addCF">Add</a>
<form action="" method="post" target="_blank">
<table id="products">
<tr><td>Enter name for item <input type="text" name="item_name[]"> <input type="checkbox" name="item_checked[]" value="yes"> <a href="javascript:void(0);" class="remCF tip" title="Remove this item">Remove</a></td></tr>
</table>
<input type="submit" value="Submit">
</form>
<?php
for($i = 0; $i < count($_POST['item_name']); $i++){
$itemName = $_POST['item_name'][$i];
$itemChecked = "no";
if(isset($_POST['item_checked'][$i]) && $_POST['item_checked'][$i] == "yes"){
$itemChecked = "yes";
}
echo "item_name: $itemName, i: $i, itemChecked $itemChecked <br />";
}
?>
TL, DR: The checkbox is not correctly posted with the item.