jeudi 8 avril 2021

Checkbox in PHP foreach loop - all checkboxes activate the 1st

I have a foreach loop providing data to a html table. I have added a form around the table and on each row am adding an input/checkbox with the value set as the row_id from the db call.

When clicking on the first checkbox it selects, when clicking on other rows the 1st checkbox in the table selects not the row you intended.

I was under the impression that setting the name="whatever[]" like so adds the checkbox to an array? So in the form processor I can get it via $_POST['checkbox'] array. Which is working on the form processor part.

I am using bootstrap to style everything so this might be an issue?

I can't for the life of me seem to be able to fix the click one checkbox get only the first checked.

This is my table code:

<form action="<?php echo URLROOT.'/admin/voyage/sendemail'; ?>" method="POST">
<table id="volunteers" class="table table-bordered text-secondary">
  <thead class="bg-msp-lightgrey">
    <tr>
      <th width="35px">

      </th>
      <th>
        Volunteer
      </th>
      <th>
        Position
      </th>
      <th>
        DBS Status
      </th>
      <th>
        Last Training
      </th>
      <th>
        Last Refit
      </th>
      <th>
        Contact
      </th>
      <th>
        Form
      </th>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($data['volunteerData'] as $volunteer) : ?>
      <?php
        $result = 'DBS Form to be sent';
        $order = 0;
        if ($volunteer->volunteer_dbsSent) {
          $result = 'DBS Form sent to volunteer - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsSent));
          $order = 1;
        }
        if ($volunteer->volunteer_dbsReceived) {
          $result = 'DBS Form returned to us - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsReceived));
          $order = 2;
        }
        if ($volunteer->volunteer_dbsASTO) {
          $result = 'DBS Form sent to ASTO - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsASTO));
          $order = 3;
        }
        if ($volunteer->volunteer_dbsReturned) {
          $result = 'DBS returned - '.date('d/m/Y', strtotime($volunteer_dbsReturned));
          $order = 4;
        }
        if ($volunteer->volunteer_dbsClear) {
          $result = 'DBS clear, cert number: - '.$volunteer->volunteer_dbsCertNum;
          $order = 5;
        }
      ?>
      <tr>
        <td>
          <div class="custom-control custom-checkbox">
            <input type="checkbox" class="custom-control-input" id="check" name="check[]" value="<?php echo $volunteer->volunteer_id; ?>" />
            <label class="custom-control-label" for="check"></label>
          </div>
        </td>
        <td>
          <a class="text-msp-lightblue" href="<?php echo URLROOT.'/admin/volunteer/manage/'.$volunteer->volunteer_id; ?>"><?php echo $volunteer->volunteer_firstName.' '.$volunteer->volunteer_lastName; ?></a>
        </td>
        <td>
          <?php echo $volunteer->volunteer_rank; ?>
        </td>
        <td data-sort="<?php echo $order; ?>">
          <?php echo $result; ?>
        </td>
        <td>

        </td>
        <td>

        </td>
        <td>
          <a class="text-msp-lightblue" href="mailto:<?php echo $volunteer->volunteer_email; ?>"><?php echo $volunteer->volunteer_email; ?></a><br />
          <a class="text-msp-lightblue" href="tel:<?php echo $volunteer->volunteer_mobile; ?>"><?php echo $volunteer->volunteer_mobile; ?></a>
        </td>
        <td>
          <a target="_blank" class="text-msp-lightblue" href="<?php echo URLROOT.'/admin/volunteer/volunteerForm/'.$volunteer->volunteer_id; ?>">View</a>
        </td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
<div class="row mt-4">
  <div class="col-5">
    <div class="input-group">
      <select class="custom-select" id="email" name="email">
        <option selected>Update DBS Status...</option>
        <option value="1">A</option>
        <option value="2">B</option>
        <option value="3">C</option>
      </select>
      <div class="input-group-append">
        <button class="btn btn-msp-lightblue text-white" type="submit"><i class="fas fa-paper-plane"></i> Send</button>
      </div>
    </div>
  </div>
</div>
</form>



Aucun commentaire:

Enregistrer un commentaire