user9236271
user9236271

Reputation:

How do I get to update my table values by clicking a button?

I have this page where the employee leaves can be approve/reject by the admin. So far this is what I have:

<body>

<?php

if(isset($_POST['approved']))
{
    echo "Approved";
    $status=$_POST['status'];
}
if(isset($_POST['rejected']))
{
    echo "Rejected";
    $status=$_POST['status'];
}
?>

<!-- Begin page content -->
<div class="container">
    <div class="page-header">

        <h3>
            Employee Leaves
        </h3>
        <div class="table-responsive">
            <table class="table">
                <tr>
                    <th>Employee Name</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>From</th>
                    <th>To</th>
                    <th>Reason</th>
                    <th>Status</th>
                    <th>---</th>
                </tr>
                <?php
                include ('database.php');
                $result = $database->prepare ("SELECT * FROM leaves order by id DESC");
                $result ->execute();
                for ($count=0; $row_message = $result ->fetch(); $count++){
                    ?>
                    <tr>
                        <td><?php echo $row_message['full_name']; ?></td>
                        <td><?php echo $row_message['phone']; ?></td>
                        <td><?php echo $row_message['email']; ?></td>
                        <td><?php echo $row_message['fromdate']; ?></td>
                        <td><?php echo $row_message['todate']; ?></td>
                        <td><?php echo $row_message['reason']; ?></td>
                        <td><?php echo $row_message['status']; ?></td>
                        <td>
                            <form method="post" action=""><input type="submit" value="Approved" name="approved"></input></form>
                            &nbsp
                            <form method="post" action=""><input type="submit" value="Rejected" name="rejected"></input></form>
                        </td>
                    </tr>
                <?php	}	?>
            </table>

            <a href="home"><button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button></a>

        </div>
    </div>
</div>
</div>

So I'm trying to get the value of status to update when I click on approve/reject.

Here's my table schema:

Table Schema

I think the last thing I need is the update query (not sure). I know there are tons of tutorials out there, I'm sorry but I just can't understand it. I think I'm gonna need a specific one.

Upvotes: 0

Views: 74

Answers (3)

Tharindu Thisarasinghe
Tharindu Thisarasinghe

Reputation: 3998

In these form elements, you need to add action (Where you process your form data).

In this case that's this page itself.

So, you can use the PHP file name or $_SERVER["PHP_SELF"] (PHP_SELF is a variable that returns the current script being executed).

So, your form elements should look like

<form method="post" action="this_file_name.php"><input type="submit" value="Approved" name="approved"></input></form>
&nbsp
<form method="post" action="this_file_name.php"><input type="submit" value="Rejected" name="rejected"></input></form>

OR

<form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>"><input type="submit" value="Approved" name="approved"></input></form>
&nbsp
<form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>"><input type="submit" value="Rejected" name="rejected"></input></form>

Upvotes: 0

Pablo
Pablo

Reputation: 6048

You don't have a SQL query to update the database record once either action button is pressed.

Here is a hint:

$stmt = $database->prepare ("UPDATE leaves SET status = :status WHERE id = :row_id");
$stmt->execute(['status' => $status, 'record_id' => $record_id]);

Also note that you only need one form and you need to include the record id in the submission:

<form method="post" action="">
   <input type="hidden" value="<?php echo $row_message['id']; ?>" />
   <input type="submit" value="Approved" name="approved"></input>
   <input type="submit" value="Rejected" name="rejected"></input>
</form>

Upvotes: 0

Ahsan Ali
Ahsan Ali

Reputation: 5135

There is no need to create a form in the loop to get the value of status. You may pass the value in an <a href='page?approved=[status]'> and get it when it's clicked.

You can do the following

<?php
for ($count=0; $row_message = $result ->fetch(); $count++){
    $id = $row_message['id']
    ?>
    <tr>
        <td><?php echo $row_message['full_name']; ?></td>
        <td><?php echo $row_message['phone']; ?></td>
        <td><?php echo $row_message['email']; ?></td>
        <td><?php echo $row_message['fromdate']; ?></td>
        <td><?php echo $row_message['todate']; ?></td>
        <td><?php echo $row_message['reason']; ?></td>
        <td><?php echo $row_message['status']; ?></td>
        <td>
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?approved=' .$row_message['status']?>">Approve</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?rejected=' .$row_message['status']?>">Rejected</a>
        </td>
    </tr>
<?php   }   ?>

and get the value of status:

<?php

if(isset($_GET['approved']))
{
    echo "Approved";
    $status=$_GET['approved'];
}
if(isset($_GET['rejected']))
{
    echo "Rejected";
    $status=$_GET['rejected'];
}
?>

Upvotes: 1

Related Questions