mardi 17 novembre 2020

html checkbox arrays generating additional array elements on a change

Long time listener, first time caller. I am implementing a system to track who participates in various events. After defining the "activity" record, the plan is to populate another table with the list of members (lets say 60) who might have attended, then present a form for the organizer to 'check' who actually participated with the hours spent. This information is then passed by POST to update the database.

The issue deals with the array of Checkboxes, one for each participant, and correct handoffs to the DB update process. The problem is that the first time a checkbox is changed, either manually by the organizer or automatically to reflect a previous choice, it generates an extra element in the checkbox array, thereby throwing them all out of alignment with the other arrays. This link (Do checkbox inputs only post data if they're checked?) was the closest to the problem that I found, but did not address handling checkbox arrays.

I implemented the hidden checkbox approach per several posts, tried both absolute array indexing, and am I still get an extra array element every time. I even contemplated replacing the checkboxes with something less elegant like 2 radio buttons (Y/N) each or a two-value select field. I am definitely not an expert here and am willing to do the research and learn, but I've hit a dead-end.

Any ideas, workarounds, or links to other references for this would be appreciated.

Here is the code:

<?php
echo "<form action=\"index.php\" method=\"post\">";
$query = ((get stuff));
$result = mysqli_query($con, $query) or die ('Sorry, could not access the joined users+activitypart tables');

// extract and format all users for the activity
$i = 0;
while($row=mysqli_fetch_array($result, MYSQL_ASSOC))
    {
    $firstname = $row['firstname'];
    $lastname = $row['lastname'];

    $apidx[] = $row['apidx'];                   // Pointer to this user's record
    $isParticipant[] = $row['isParticipant'];   // checkbox value   
    $actduration[] = $row['actduration'];       // Actual duration in hours spent
    $role[] = $row['role'];                     // Role that this person held during this activity

?>
    <input type="hidden" name="passidx[]" value="<? print $apidx[$i] ?>">   

    <input type="hidden" name="passpart[<? $i ?>]" value="0" />
    <input type="checkbox" name="passpart[<? $i ?>]" value="1" <? if($isParticipant["$i"]) print "CHECKED" ?> />
    
    <input type="text" name="passdura[]" size="7" value="<? print $actduration["$i"] ?>">

<?php   
    echo "$i $firstname $lastname<br>";
    $i++;
    }
?>

    <!-- when done, pass all name field values to actusr08a  -->
    <br><input type="hidden" name="content" value="actusr08a">

    <span  class="form-field-no-caption">
        <input type="submit" value="Submit">
    </span>
</form>



Aucun commentaire:

Enregistrer un commentaire