我使用php从数据库中打印一些信息。我使用html以表格格式显示它。每行有3个按钮,用户可以在其中更改该特定行中的课程。问题是我不能定义那个表中的所有按钮。如果它是一个提交按钮,我会给它一个名称,然后通过$_POST[‘name’]获取数据,但对于多行,我感到困惑。这是我的代码:
for($i=0;$i<$myarray_lenght;$i++)
{
$row_id=$i;
echo "<tr>";
echo "<td><form action='private_page.php' method='POST'> <input type='radio' name=$row_id value='edit'> </form> </input></td>";
echo "<td>". $myarr[$i]['thesis_name']. "</td>".
"<td>" .$myarr[$i]['student_id']."</td>"
echo "<td><form action='private_page.php' method='POST'> <input type='submit' name='more_info' value='Get more info of the Student !'> </form></input></td>";
echo "<td><form action='private_page.php' method='POST'> <input type='submit' name='remove' value='Remove the Student !'> </form></input></td>";
echo "</tr>";
}
我试着通过编辑按钮来识别每一行,然后说,每当点击该行(单选按钮)时,都会从该行中获得如下信息:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted
for($i=0;$i<100;$i++)
if (isset($_POST[$i])) {
if (isset($_POST['more info'])) {
// do something.
}
首先,<form>
不允许是<table>
、<tbody>
、<tr>
或<td>
的子元素。您可以在<form>
中包含一个完整的<table>
。您可以在<table> cell
中包含<form>
。<form>
中不能包含<table>
的一部分。检查表内部
因此,我不建议您在<table>
中使用<form>
。
备选方案/建议
1) 第一个解决方案
<?php
for($i=0;$i<$myarray_lenght;$i++)
{
$row_id=$i;
$remove = "Remove";
$more_info = "more_info";
echo "<tr>";
echo "<td></td>";
echo "<td>". $myarr[$i]['thesis_name']. "</td>"."<td>" .$myarr[$i]['student_id']."</td>"
echo "<td>".
"<a href='private_page.php?id=$passStudentId&action=$more_info'>".
"<input type='button' name='more_info' value='Get more info of the Student !'></input>".
"</a>".
"</td>";
echo "<td>".
"<a href='private_page.php?id=$passStudentId&action=$remove'>".
"<input type='button' name='remove' value='Remove the Student !'></input>".
"</a>".
"<td>";
echo "</tr>";
}
?>
private_page.php
<?php
$studentID = $_GET['id'];
$action = $_GET['action'];
if($action == "Remove") {
// Do The Needfull action
}
if($action == "more_info") {
// Do The Needfull action
}
?>
或
2)第二个解决方案
<form method='POST' action='private_page.php'>
<table>
<?php
for($i=0;$i<$myarray_lenght;$i++)
{
$row_id=$i;
echo "<tr>";
echo "<td><input type='radio' name='student' value='."$studentID".'></td>";
echo "<td>". $myarr[$i]['thesis_name']. "</td>"."<td>" .$myarr[$i]['student_id']."</td>"
echo "</tr>";
}
?>
</table>
<input type='submit' name='submit' value='more_info'></input>
<input type='submit' name='submit' value='Remove'></input>
</form>
private_page.php
<?php
$studentID = $_POST['student'];
$action = $_POST['submit'];
if($action == "Remove") {
// Do The Needfull action
}
if($action == "more_info") {
// Do The Needfull action
}
?>
对于您的每个表单,添加一个隐藏的输入,其值设置为您当前的id-
echo "<td><form action='private_page.php' method='POST'>";
echo "<input type='submit' name='more_info' value='Get more info of the Student !'>";
echo "<input type='hidden' name='thisID' value='". $myarr[$i]['student_id'] ."'/>";
echo "</form></input></td>";
然后在您的private_page.php中,
$id = $_POST['thisID'];
$action = (isset($_POST['remove'])) ? "remove" : "more info";
// do your $action on your $id....
您的另一个选择是使用Javascript构建具有适当id和操作的post请求。