我使用html + php创建了一个动态表,输入就像一个表单(这是现实中的矩阵)和 我想知道是否可以恢复用户在动态表格中输入的数据?这是我的代码:
<?php
$rows = 3; // define number of rows
echo ' <form action="f.php" method="post">';
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
echo "<th> E".$tr." </th>";
for($td=1;$td<=$rows;$td++){
echo '<td><input type="number" name="etat" placeholder="nb d etat" /></td>';
}
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" value="Create Table">';
echo '</form>'
?>
答案 0 :(得分:1)
是的,但是您必须通过提供行号和列号来创建表单,因为您要创建矩阵:
$rows = 3; // define number of rows
echo ' <form action="f.php" method="post">';
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
echo "<th> E".$tr." </th>";
for($td=1;$td<=$rows;$td++){
echo '<td><input type="number" name="etat_'.$tr.'_'.$td.'" placeholder="nb d etat" /></td>';
}
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="submit" value="Create Table">';
echo '</form>';
在 f.php 获取数据中:
if(isset($_POST['submit'])) {
print_r($_POST);
}
它为您提供输出:
Array
(
[etat_1_1] => 1 //means 1st row 1st column
[etat_1_2] => 2 //means 1st row 2nd column
[etat_1_3] => 3 //means 1st row 3rd column
[etat_2_1] => 4 //means 2nd row 1st column and so on...
[etat_2_2] => 5
[etat_2_3] => 6
[etat_3_1] => 7
[etat_3_2] => 8
[etat_3_3] => 9
[submit] => Create Table
)