如何在按钮单击中获取行ID?

时间:2012-05-10 01:43:55

标签: php html

我有一张表,我填写了数据库中的记录。单击视图按钮时我想要做的是能够获取行的ID,以便我可以调用用户的其他信息,但我不太确定如何操作。这是我填写表格的方式。

 <table border="1" cellpadding="5" cellspacing="2" width="600">
<tr>
    <th>ID</th>
    <th>Username</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>Date Registered</th>
    <th>Member Details</th>
</tr>

 <?php
require('db_connection.php');
$query="SELECT ID,Username,Email,Telephone,RegisterDate FROM Members";
$result=mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_array($result))
{
    echo "</td><td>";
    echo $row['ID'];
    echo "</td><td>";
    echo $row['Username'];
    echo "</td><td>";
    echo $row['Email'];
    echo "</td><td>";
    echo $row['Telephone'];
    echo "</td><td>";
    echo $row['RegisterDate'];
    echo "</td><td>";
    print '<center><input name="ViewBtn" type="submit" value="View" /></center>';
    echo "</td></tr>";
}
?>

image http://s17.postimage.org/fmwtnjcwv/List_Members.png

3 个答案:

答案 0 :(得分:2)

你可以为每个按钮制作小形式,但我更喜欢超链接,然后我将其设置为按钮样式:

<style type="text/css">
  .buttonize {
    text-decoration: none;
    border: 1px solid #ccc;
    background-color: #efefef;
    padding: 10px 15px;
    -moz-border-radius: 11px;
    -webkit-border-radius: 11px;
    border-radius: 11px;
    text-shadow: 0 1px 0 #FFFFFF;
  }
</style>

<table border="1" cellpadding="5" cellspacing="2" width="600">
<tr>
    <th>ID</th>
    <th>Username</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>Date Registered</th>
    <th>Member Details</th>
</tr>

 <?php
require('db_connection.php');
$query="SELECT ID,Username,Email,Telephone,RegisterDate FROM Members";
$result=mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_array($result))
{
    echo "</td><td>";
    echo $row['ID'];
    echo "</td><td>";
    echo $row['Username'];
    echo "</td><td>";
    echo $row['Email'];
    echo "</td><td>";
    echo $row['Telephone'];
    echo "</td><td>";
    echo $row['RegisterDate'];
    echo "</td><td>";
    print '<center><a href="view.php?id='.$row['ID'].'" class="buttonize">View</a></center>';
    echo "</td></tr>";
}
?>

你可以用CSS做更多的事情,但这应该给你正确的想法。

答案 1 :(得分:2)

通过按钮发送您的行ID HTML

<button type="submit"  id="" title="" onClick="high('a<?php echo $i;?>')"  >

JS:

使用js来捕捉id

function high(id)
{
        alert(id);
}

答案 2 :(得分:1)

您可以使用隐藏的输入元素,如下所示:

"<input type=hidden id='rowid' value=".row['ID'].">";e