Capturing the value from multiple checkboxes with same value
<p>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).</p>

<p>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:</p>

<pre><code>&lt;form action="" method="post" target="_blank"&gt;
Enter name for item 1 &lt;input type="text" name="item_name[]" required&gt;  &lt;input type="checkbox" name="item_checked[]" value="yes"&gt; &lt;br /&gt;
Enter name for item 2 &lt;input type="text" name="item_name[]" required&gt;  &lt;input type="checkbox" name="item_checked[]" value="yes"&gt; &lt;br /&gt;
Enter name for item 3 &lt;input type="text" name="item_name[]" required&gt;  &lt;input type="checkbox" name="item_checked[]" value="yes"&gt; &lt;br /&gt;
&lt;input type="submit" value="Submit"&gt;
&lt;/form&gt;

&lt;?php
for($i = 0; $i &lt; count($_POST['item_name']); $i++){
    $itemName = $_POST['item_name'][$i];
    $itemChecked = "no";
    if(isset($_POST['item_checked'][$i]) &amp;&amp; $_POST['item_checked'][$i] == "yes"){
        $itemChecked = "yes";   
    }
    echo "item_name: $itemName, i: $i, itemChecked $itemChecked &lt;br /&gt;";
}

?&gt;
</code></pre>

<p>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 <code>$i</code> is different and this it is set for the first item).</p>

<p>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:</p>

<pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function(){
    $(".addCF").click(function(){
        $("#products").append('&lt;tr&gt;&lt;td&gt;Enter name for item &lt;input type="text" name="item_name[]"&gt; &lt;input type="checkbox" name="item_checked[]" value="yes"&gt; &lt;a href="javascript:void(0);" class="remCF tip" title="Remove this item"&gt;Remove&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;');
    });
    $("#products").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});
&lt;/script&gt;


&lt;a href="javascript:void(0);" class="addCF"&gt;Add&lt;/a&gt;


&lt;form action="" method="post" target="_blank"&gt;

&lt;table id="products"&gt;
&lt;tr&gt;&lt;td&gt;Enter name for item &lt;input type="text" name="item_name[]"&gt;  &lt;input type="checkbox" name="item_checked[]" value="yes"&gt; &lt;a href="javascript:void(0);" class="remCF tip" title="Remove this item"&gt;Remove&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;/table&gt;

&lt;input type="submit" value="Submit"&gt;
&lt;/form&gt;


&lt;?php
for($i = 0; $i &lt; count($_POST['item_name']); $i++){
    $itemName = $_POST['item_name'][$i];
    $itemChecked = "no";
    if(isset($_POST['item_checked'][$i]) &amp;&amp; $_POST['item_checked'][$i] == "yes"){
        $itemChecked = "yes";   
    }
    echo "item_name: $itemName, i: $i, itemChecked $itemChecked &lt;br /&gt;";
}

?&gt;
</code></pre>

<p>TL, DR: The checkbox is not correctly posted with the item.</p>