Ritzel
Ritzel

Reputation: 33

How to get row ID in button click?

I have a table that I fill with records from the database. What I want to do when the view button is clicked is to be able to get the ID of the row so that I can call on the other information of the user but I'm not quite sure how to do it. Here's how I filled the table.

 <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

Upvotes: 0

Views: 21384

Answers (3)

Piyush
Piyush

Reputation: 4007

Send your row ID via button HTML

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

JS:

Use js to catch id

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

Upvotes: 2

Mario
Mario

Reputation: 11

You could use a hidden input element, like this:

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

Upvotes: 1

John Conde
John Conde

Reputation: 219794

You could make little forms for each button but I prefer hyperlinks that I then style to look like buttons:

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

You can do more with the CSS but that should give you the right idea.

Upvotes: 2

Related Questions