Morning All,
I have a form that I am using to add multiple rows into a database at once. It all seems to be working fine, except for the checkboxes. There is one checkbox for each form row. Regardless of what order the checkboxes are ticked, the results are always returned in the same order after being submitted.
Here is the main part of the form that matters:
<tr>
<td class='border'>
<input type='text' name='rowid[]' value='".$row."' style='display:none;' />
<input type='text' name='ref[]' style='width:100%;' />
</td>
<td class='border'>
<input type='text' name='obs[]' style='width:100%;' />
</td>
<td class='border'>
<input type='text' name='act[]' style='width:100%;' />
</td>
<td class='border' align='center'>
<input type='checkbox' name='comp[]' value='Yes' />
</td>
<td class='border'>
<input type='text' name='compby[]' style='width:100%;' />
</td>
<td class='border'>
<input type='text' name='compdate[]' id='completeddate".$row."' class='completeddate' style='width:100%;' />
</td>
</tr>
This form gets submitted, and my PHP code deals with it. You can see that the very first input, is the ID of the row of the form. This is a hidden field and the value is incremented by one for each row that is displayed in the form.
Once the form is submitted, I use the below PHP to get deal with each row, one at a time
foreach($_POST['rowid'] as $key=>$rowid) {
This should basically say that for a rowid, the rest of the inputs are associated, until it gets to the next rowid.
If I were to then print say
echo $_POST['ref'][$key];
I will get the reference for the first row, and then once the loop completes and prints it again, I will get the reference for the second row, and so on.
However, if I were to put
echo $_POST['comp'][$key];
Then this is where it breaks.
Let's say for example, that for each row I ticked the checkboxes in this order:
row 1, tick, row 2, untick, row 3, tick
If I then let the loop print out all of the checkbox values, it will display
'Yes', 'Yes', ''
But it should be in this order
'Yes', '', 'Yes'
Is this because the checkbox is normal an array anyway, and I am then putting this into another array by giving it a name of comp[]?
Is there anyway around this issue, or would I be better off having radio buttons, one for yes, and one for no?
Many thanks
Eds