php - 在php中选择特定行中的特定按钮

标签 php html mysql row

我使用 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/

相关文章:

MySql 为搜索过滤器准备sql语句

php - 递归检查数据库中 child 的 parent

唯一 varchar 字段与唯一 bigint 的 MySQL 性能

javascript - 通过 AJAX 使用 JQuery 获取 HTML id

php - 最佳选择? MySQL 查询 Action

html - 如何修复页面中心的模态弹出窗口

css - 如何使用 CSS 将两个 span 合并为一行

php - XPath 不断返回空节点列表

php - 语法检查 API?

html - 如何定位按钮使其与标题一致?