我使用 php 从我的数据库中打印一些信息。我使用 html 以表格格式显示它。在每一行都有 3 个按钮,用户可以在该特定行中更改该类(class)。问题是我不能定义该表中的所有按钮。如果它是单个提交按钮,我会给它一个名称,然后通过 $_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.
}
最佳答案
首先,A <form>
不允许成为 <table>
的子元素, <tbody>
或 <tr>
或 <td>
.你可以拥有一个完整的 <table>
在 <form>
里面.你可以有一个<form>
在 <table> cell
里面.您不能拥有 <table>
的一部分在 <form>
里面.检查Form Inside Table
所以,我不会建议你使用<form>
里面<table>
.
替代/建议
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
}
?>
关于php - 在php中选择特定行中的特定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37144160/