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

时间:2016-05-10 16:29:30

标签: php html mysql row

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

  }

2 个答案:

答案 0 :(得分:1)

对于每个表单,添加一个隐藏的输入,其值设置为您当前的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和操作的发布请求。

答案 1 :(得分:1)

首先,不允许<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
}
?>

OR

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
}
?>