Appearance
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.
Example of an HTML table that has 3 columns and 2 rows inside a form
html
<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>
<div class="alert alert-info flex not-prose">Watch a video course Learn object oriented PHP
</div>
- In the PHP script that the form submits to, you can access the data from the table using the
$_POSTvariable. The table data will be stored in the$_POST['data']variable as a multi-dimensional array.
Example of submitting html table data via form (post) in PHP
php
<?php
$data = $_POST['data'];
// loop through the rows
foreach ($data as $row) {
// loop through the cells in each row
foreach ($row as $cell) {
echo htmlspecialchars($cell, ENT_QUOTES, 'UTF-8') . "<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. You should add validation and sanitization (like htmlspecialchars()) to your code before using the data from the form.