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

Time:09-06

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 >
  <thead>
    <th  scope="col">Code</th>
    <th  scope="col">Name</th>
    <th  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?

CodePudding user response:

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 >
  <thead>
    <th  scope="col">Code</th>
    <th  scope="col">Name</th>
    <th  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