0

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>
Matthew Barraud
  • 467
  • 1
  • 5
  • 17
  • Your first question answer is YES, when you use [], you can get data in an array, Second question is account bootstrap styling that is not an issue for use. – Pooya Sabramooz Apr 08 '21 at 09:35
  • @PooyaSabramooz, as I thought, thanks; do you know why only the first checkbox activates when clicking on the other rows? – Matthew Barraud Apr 08 '21 at 09:39
  • do you want to select only one checkbox in the table? – Vel Apr 08 '21 at 09:39
  • All your checkboxes created in that loop will all have the same `id="check"`, which is invalid in HTML. Id's _must_ be unique within a document (no reuse of id's at all) – M. Eriksson Apr 08 '21 at 09:43
  • I want to be able to select one or many depending on the action I want to do. The intention is to get the row id's from the value="" part and do an action based on the select. For example, send message a to rows 1, 5, 12, or even send message b to row 3 – Matthew Barraud Apr 08 '21 at 09:43
  • 1
    @MagnusEriksson, That was it! I have added the row ID to the id= part and I can now select each in turn, I assume that the name="xyz" is the variable I call in $_POST['xyz']? – Matthew Barraud Apr 08 '21 at 09:46
  • Exactly. The `id` attribute is so you can reference specific HTML elements on the page (like when using JS) and the `name` attribute is the name of the field when it's submitted. – M. Eriksson Apr 08 '21 at 09:48
  • Does this answer your question? [Html duplicated ID](https://stackoverflow.com/questions/22059438/html-duplicated-id) – M. Eriksson Apr 08 '21 at 09:50

1 Answers1

0

Try this code. Added new variable $type= ""; and passed in checkbox array.

<input type="checkbox" class="custom-control-input" id="check_<?php echo $Inc;?>" name="check[<?php echo $type;?>][]" value="<?php echo $volunteer->volunteer_id; ?>" />

Checkbox Id also made unique.

      <tbody>
        <?php 
        $Inc = 1;
        foreach ($data['volunteerData'] as $volunteer) : ?>
          <?php
            $result = 'DBS Form to be sent';
            $order = 0;
            $type= "";
            if ($volunteer->volunteer_dbsSent) {
              $result = 'DBS Form sent to volunteer - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsSent));
              $order = 1;
              $type= "dbsSent";
            }
            if ($volunteer->volunteer_dbsReceived) {
              $result = 'DBS Form returned to us - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsReceived));
              $order = 2;
              $type= "dbsReceived";
            }
            if ($volunteer->volunteer_dbsASTO) {
              $result = 'DBS Form sent to ASTO - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsASTO));
              $order = 3;
              $type= "dbsASTO";
            }
            if ($volunteer->volunteer_dbsReturned) {
              $result = 'DBS returned - '.date('d/m/Y', strtotime($volunteer_dbsReturned));
              $order = 4;
              $type= "dbsReturned";
            }
            if ($volunteer->volunteer_dbsClear) {
              $result = 'DBS clear, cert number: - '.$volunteer->volunteer_dbsCertNum;
              $order = 5;
              $type= "dbsClear";
            }
          ?>
          <tr>
            <td>
              <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" id="check_<?php echo $Inc;?>" name="check[<?php echo $type;?>][]" 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 
            $Inc++;
        endforeach; ?>
      </tbody>
Vel
  • 9,027
  • 6
  • 34
  • 66