PHP - Capture Table Data During Form Submission that has no Inputs

Viewed 32

I have a simple web form that also includes a table that allows users to delete table rows before submitting the form. I need to capture the data from the table rows that exist when the form is submitted but as there are no form input fields in the table these are not showing in my request variables.

The table is pretty simple and looks like this:

<table class="table table-condensed table-striped table-bordered">
  <thead>
    <th class="text-center" scope="col">Code</th>
    <th class="text-center" scope="col">Name</th>
    <th class="text-center" scope="col">Part ID</th>

  </thead>
  <tbody>

    <tr>
      <td>ABC123</td>
      <td>Widgets - Small</td>
      <td>S2000</td>
    </tr>
    <tr>
      <td>ZXR987</td>
      <td>Bolts - Large</td>
      <td>G5600</td>
    </tr>


  </tbody>
</table>

I need to capture the value for the Part ID for each table row that is left when the form is submitted but not sure if this is possible or how to go about it?

1 Answers

The simplest method is to add a hidden field with the Part ID. In the example below $_POST['partId'] will return an array of the part ids left.

<table class="table table-condensed table-striped table-bordered">
  <thead>
    <th class="text-center" scope="col">Code</th>
    <th class="text-center" scope="col">Name</th>
    <th class="text-center" scope="col">Part ID</th>

  </thead>
  <tbody>

    <tr>
      <td>ABC123</td>
      <td>Widgets - Small</td>
      <td>S2000 <input type="hidden" name="partId[]" value="S2000"></td>
    </tr>
    <tr>
      <td>ZXR987</td>
      <td>Bolts - Large</td>
      <td>G5600 <input type="hidden" name="partId[]" value="G5600"></td>
    </tr>


  </tbody>
</table>

Related