Php 如何使用SQL查询生成的按钮在单击时获取id/名称?

Php 如何使用SQL查询生成的按钮在单击时获取id/名称?,php,mysql,button,html-table,Php,Mysql,Button,Html Table,我正在从MySQL数据库创建一个表。目前,我可以手动输入课程ID,然后单击“选择”以根据输入的课程ID显示花名册 但是,我希望通过在每行后面的表格右侧的按钮来简化此过程。最好的方法是什么?我可以提供一个截图,但我仍然需要更多的声誉 因此,生成此表的代码如下所示 <?php // Connection information to database has been removed. // $Row[1] - first name of faculty // $Row[2] - last

我正在从MySQL数据库创建一个表。目前,我可以手动输入课程ID,然后单击“选择”以根据输入的课程ID显示花名册

但是,我希望通过在每行后面的表格右侧的按钮来简化此过程。最好的方法是什么?我可以提供一个截图,但我仍然需要更多的声誉

因此,生成此表的代码如下所示

<?php
// Connection information to database has been removed. 

// $Row[1] - first name of faculty
// $Row[2] - last name of faculty
// $fieldRow[0] - course ID
// $fieldRow[1] - course name
// $fieldRow[2] - course year
// $fieldRow[3] - number of units

echo "<table>
<caption>....for " . $Row[1] . " " . $Row[2] . "</caption>
<tr>
  <th>Course ID</th>
  <th>Course Name</th>
  <th>Course Year</th>
  <th>Units</th>
</tr>";
while ($fieldRow = mysqli_fetch_row($result2))
{
    echo "<tr><td>" . $fieldRow[0] . "</td>
    <td>" . $fieldRow[1] . "</td>
    <td>" . $fieldRow[2] . "</td>
    <td>" . $fieldRow[3] . "</td>
    <td><button name='" . $fieldRow[0] . "'>Select</button></td>
    </tr>";
}
echo "</table>";
?>

<form method="get" action="class.php">
  <p>Enter Course ID
    <input autofocus="autofocus" tabindex="1" type="text" maxlength="5" name="classSelect" title="Enter Class Number" />
  </p>
  <p>
    <button tabindex="2" type="submit">Select</button>
  </p>
</form>

你可以有

if(isset($_GET['id'])
{
  $course_id = intval($_GET['id']);
  // Do your query against the course id.
}

 while ($fieldRow = mysqli_fetch_row($result2))
    {
      echo "<tr><td>" . $fieldRow[0] . "</td>
      <td>" . $fieldRow[1] . "</td>
      <td>" . $fieldRow[2] . "</td>
      <td>" . $fieldRow[3] . "</td>
      <td><a href='" . ?id=$fieldRow[0] . "'>Select</a></td>
      </tr>";
    }
    echo "</table>";
    ?>
if(isset($\u GET['id']))
{
$course\u id=intval($\u GET['id']);
//对课程id执行查询。
}
而($fieldRow=mysqli\u fetch\u行($result2))
{
回显“%$fieldRow[0]”
“$fieldRow[1]”
“$fieldRow[2]”
“$fieldRow[3]”
";
}
回声“;
?>

您可以使用JQuery来实现这一点

在代码中

echo "<tr><td>" . $fieldRow[0] . "</td>
<td>" . $fieldRow[1] . "</td>
<td>" . $fieldRow[2] . "</td>
<td>" . $fieldRow[3] . "</td>
<td><button name='" . $fieldRow[0] . "' class='selecter'>Select</button></td>
</tr>";
echo.“$fieldRow[0]”
“$fieldRow[1]”
“$fieldRow[2]”
“$fieldRow[3]”
挑选
";
再加上这样一个头

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".selecter").click(function(){
        $("input").val($(this).attr("name"));
    });
});
</script>
</head>

$(文档).ready(函数(){
$(“.selector”)。单击(函数(){
$(“输入”).val($(this.attr(“名称”));
});
});
完整代码

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".selecter").click(function(){
        $("input").val($(this).attr("name"));
    });
});
</script>
</head>
<body>
 <?php
    // Connection information to database has been removed. 

    // $Row[1] - first name of faculty
    // $Row[2] - last name of faculty
    // $fieldRow[0] - course ID
    // $fieldRow[1] - course name
    // $fieldRow[2] - course year
    // $fieldRow[3] - number of units

 echo "<table>
<caption>....for " . $Row[1] . " " . $Row[2] . "
</caption>
    <tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Course Year</th>
<th>Units</th>
</tr>";
    while ($fieldRow = mysqli_fetch_row($result2))
    {
      echo "<tr><td>" . $fieldRow[0] . "</td>
<td>" . $fieldRow[1] . "</td>
<td>" . $fieldRow[2] . "</td>
<td>" . $fieldRow[3] . "</td>
<td><button name='" . $fieldRow[0] . "' class='selecter'>Select</button></td>
</tr>";
    }
    echo "</table>";
    ?>

    <form method="get" action="class.php">
    <p>Enter Course ID
      <input autofocus="autofocus" tabindex="1" type="text" maxlength="5" name="classSelect" title="Enter Class Number" />
    </p>
    <p>
      <button tabindex="2" type="submit">Select</button>
    </p>
    </form>
</body>
</html>

$(文档).ready(函数(){
$(“.selector”)。单击(函数(){
$(“输入”).val($(this.attr(“名称”));
});
});