如何使用从 SQL 查询生成的按钮获得 id/name onclick?
How would I get an id/name onclick using buttons generated from an SQL query?
我正在从 MySQL 数据库创建 table。目前,我可以手动输入Course ID,然后点击'select',根据输入的Course ID显示花名册。
不过,我想在每一行之后的 table 右侧使用按钮来简化此操作。做这个的最好方式是什么?我可以提供截图,但我仍然需要更多的声誉。
所以生成这个table的代码如下。
<?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>";
?>
您可以使用 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>";
然后像这样添加头像
<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>
完整代码
<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>
我正在从 MySQL 数据库创建 table。目前,我可以手动输入Course ID,然后点击'select',根据输入的Course ID显示花名册。
不过,我想在每一行之后的 table 右侧使用按钮来简化此操作。做这个的最好方式是什么?我可以提供截图,但我仍然需要更多的声誉。
所以生成这个table的代码如下。
<?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>";
?>
您可以使用 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>";
然后像这样添加头像
<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>
完整代码
<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>