0

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>

So, When I load this page with 15 members, as a debug step, I count the 3 arrays and they all show 15, 15, 15 elements each at load time. If I click on 2 check-boxes, post it to the next page and do a count of the received arrays, two of the arrays (passidx, passdura) show 15 elements each, and the checkbox array (passpart) shows 17. since 2 checkboxes changed, seems they then post the change, thus the additional 2 elements. This is what I am trying to fix.

Jimo
  • 1
  • 1
  • The above code is unrelated to your problem, if you want to replace the record in the DB, use `UPDATE` rather than `INSERT INTO` – shingo Nov 18 '20 at 06:29
  • UPDATE is what I intend to do in the DB update step. Its dealing with the extra 2 checkbox array elements that's the sticking point. – Jimo Nov 18 '20 at 16:19

0 Answers0