So, my webpage has a html-table which displays rows with one item pr. row including various data regarding that item in the columns.
The table is populated using mysqli call to db. So far so good.
Excerpts of the table look like this (I've tried to only show the "important" parts of the code to avoid cluttering - there 8 more text fields similar to "nytAntalGangeUdfoert" for each item)
<td><input type="text" name="nytAntalGangeUdfoert[]" value="<?php echo $antalGangeUdfoert ?>"></td>
<?php if ($aktivStatus == 1) { ?>
<td><input type="checkbox" name="nyAktivStatus[]" value="<?php echo $aktivStatus ?>" checked="checked"/></td>
<?php } else { ?>
<td><input type="checkbox" name="nyAktivStatus[]" value="<?php echo $aktivStatus ?>" /></td>
The text field(s) works fine in all aspects.
For the checkbox, the if-else part decides whether the checkbox should be displayed as checked (active or not) dependent on the value in my db-table (1 or 0) This works fine.
I then have 'duplicate' hidden fields to log the original status of the checkbox for later comparison after posting:
<td><input type="hidden" name="antalGangeUdfoert[]" value="<?php echo $antalGangeUdfoert ?>"></td>
<td><input type="hidden" name="aktivStatus[]" value="<?php echo $aktivStatus ?>"></td>
All of this is posted to another page where it is handled as follows (again, only excerpts of the full table). 'aktivstatus' is the old status from db, and 'nyaktivstatus' is the one posted (i.e. the one selected by the user via the checkboxes - can of course be the same as original state)
if(isset($_POST['submit']))
{
$antalGangeUdfoert = $_POST["antalGangeUdfoert"];
$nytAntalGangeUdfoert = $_POST["nytAntalGangeUdfoert"];
$aktivStatus = $_POST["aktivStatus"];
foreach( $navn as $n => $n )
{
if(isset($_POST["nyAktivStatus"][$n])) {
$nyAktivStatus[$n] = 1;
} else {
$nyAktivStatus[$n] = 0;
}
if( $antalGangeUdfoert[$n] <> $nytantalGangeUdfoert[$n] || $aktivStatus[$n] <> $nyAktivStatus[$n]) {
//run function to update items where a change has been made by the user)
As stated earlier, the above approach works fine for all the text fields, but in some cases, the new state of a checkbox is saved for the wrong item, apparently (changes to the text fields are correctly saved in db regardless)
Examples below:
Row---status when fetched---user input---saved as
Row1: 1-->0-->1 ERROR
Row2: 1-->1-->1 OK
Row3: 1-->1-->0 ERROR
Row1: 1-->1-->1 OK
Row2: 1-->1-->1 OK
Row3: 0-->1-->1 OK
Row1: 1-->0-->0 OK
Row2: 1-->0-->0 OK
Row3: 1-->0-->0 OK
Row1: 0-->1-->1 OK
Row2: 0-->1-->1 OK
Row3: 0-->1-->1 OK
Row1: 1-->0-->1 ERROR
Row2: 1-->0-->0 OK
Row3: 1-->1-->0 ERROR
FYI at one point, I changed the checkbox into a textfield and manually entered 1 or 0, and this worked fine...not ideal solution, though...
So, I guess there's something going wrong when posting/assigning the "nyaktivstatus' (user entered status)
Can anyone spot the issue in my code? Or spot a pattern that I'm not seeing?
Thanks!