Submit html table data with via form (post)
Here is an example of how to submit an HTML table data via a form (using the POST method) using PHP:
- Create an HTML form that contains a table with input fields for each cell. In this example, the table has 3 columns and 2 rows.
<form action="submit-table.php" method="post">
<table>
<tr>
<td><input type="text" name="data[1][1]"></td>
<td><input type="text" name="data[1][2]"></td>
<td><input type="text" name="data[1][3]"></td>
</tr>
<tr>
<td><input type="text" name="data[2][1]"></td>
<td><input type="text" name="data[2][2]"></td>
<td><input type="text" name="data[2][3]"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
Watch a video course
Learn object oriented PHP
- In the PHP script that the form submits to, you can access the data from the table using the $_POST variable. The table data will be stored in the $_POST['data'] variable as a multi-dimensional array.
<?php
$data = $_POST['data'];
// loop through the rows
foreach ($data as $row) {
// loop through the cells in each row
foreach ($row as $cell) {
echo $cell . "<br>";
}
}
?>
- The above code will display the contents of each cell of the table. You can then use this data to insert it into a database or perform other actions as needed.
Note that this is just an example and you may want to add validation and sanitization to your code before using the data from the form.