MichaelvdNet
MichaelvdNet

Reputation: 1074

PHP Button on each row of table

im making some application form in PHP.

im putting all info returned from the database into a table.

Now i want to create a button on each line that changes something in the DB of that line.

but i have no idea to do that :S

Thank you!

echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";

while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['age'] . "</td>";
    echo "<td>" . $row['position'] . "</td>";
    echo "<td>" . $row['exp'] . "</td>";
    echo "<td>" . $row['motivation'] . "</td>";
    echo "<td>" . $row['date'] . "</td>";
    echo "<td>" . $row['status'] . "</td>";
    echo "<td>" . '<input type="submit" name="submit" value="accept">' . "</td>";
    echo "</tr>";

}
    echo "</table>";

EDIT: get it working using another script:

echo "<td><a href=\"edit.php?id=".$row['id']."&status=app\">Approve</a></td>";

and edit.php:

<?php
include("dbconnect.php");
$member_id = $_GET['id'];
$status = $_GET['status'];
echo $member_id;
echo $status;
if ($status == 'app')
    $query = "update apps set status = 'approved' where id = $member_id";
mysql_query($query) or die (mysql_error());  
?>

Upvotes: 1

Views: 7588

Answers (2)

bharti88
bharti88

Reputation: 11

echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";

while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['age'] . "</td>";
    echo "<td>" . $row['position'] . "</td>";
    echo "<td>" . $row['exp'] . "</td>";
    echo "<td>" . $row['motivation'] . "</td>";
    echo "<td>" . $row['date'] . "</td>";
    echo "<td>" . $row['status'] . "</td>";
    echo "<td>" . '<form type="POST"><input type="hidden" name="whatever" value="$row['id']"><input type="submit" name="submit_btn" value="accept"></form>' . "</td>";
    echo "</tr>";

}
    echo "</table>";

in this way, you could get a form in each row of the table. Now, you just need to use php POST function.

if(isset($_POST['submit_btn']))
{
//whatever u need to do
}

Upvotes: 1

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

Create small form with parameters in the cell you want the button to do smth. This is the simpliest approach (approach with refresh).

One more solution is to use AJAX on button click and forward action to some endpoint. This way it would be dynamic and probably what you are want to implement.

Upvotes: 2

Related Questions