0

I am making a Leave Management System using PhP-Mysql.

I have a table which takes input from user while applying for their leaves. (name, leavetype, fromdate, todate, supervisor, reason and status). Only the status column has a predefined value 'pending'.

Now I want to introduce two buttons (Accept/Reject) on each row. Which on click will change the value for Status field.

I am not sure how to do it, I have tried updating the table column but it updates only if there is a where Condition, which will not be the correct procedure for such case.

<div id="content">   
<?php
$connect = new mysqli("127.0.0.1","root","","leavedb"); 
$sql = "SELECT 
name,
leavetype,
fromdate,
todate,
supervisor,
reason,
DATEDIFF(todate,fromdate) as Days,
status as Status
FROM leavereq";
$result = $connect->query($sql);    
?>

<table id="myTable">
    <tr>                                                
        <th>Name</th>
        <th>Leave Type</th>   
        <th>From Date</th>
        <th>To Date</th>
        <th>Supervisor</th>
        <th>Reason</th>
        <th>No. of Days</th>
        <th>Status</th>
    </tr>                                   

    <?php
        while ($report=$result->fetch_assoc()) 
        {                                 
        echo "<tr>";  
        echo "<td>".$report['name']."</td>"; 
        echo "<td>".$report['leavetype']."</td>";
        echo "<td>".$report['fromdate']."</td>";
        echo "<td>".$report['todate']."</td>";
        echo "<td>".$report['supervisor']."</td>";
        echo "<td>".$report['reason']."</td>";
        echo "<td>".$report['Days']."</td>";
        echo "<td>".$report['Status']."</td>";
        echo "<td>" . '<input type="submit" name="approve" value="Approve">' . "</td>";
        echo "<td>" . '<input type="submit" name="reject" value="Reject">' . "</td>";                                
        }
    ?>
</table>
</div>

1 Answer 1

1
//In the html : You have to add unique id for every <td> of status & also wants to change the input type of approve & reject...also require javascript
// check below
<script>
function function_name(status_id,req)
{
var status;
status='status'+status_id;
if(req=='approve')
{
document.getElementById(status).innerHTML='approve';
//pass ajax call to update entry in db
}
else if(req=='reject')
{
document.getElementById(status).innerHTML='reject';
//pass ajax call to update entry in db
}
</script>

<table id="myTable">
    <tr>                                                
        <th>Name</th>
        <th>Leave Type</th>   
        <th>From Date</th>
        <th>To Date</th>
        <th>Supervisor</th>
        <th>Reason</th>
        <th>No. of Days</th>
        <th>Status</th>
    </tr>                                   

    <?php
$i=0;
        while ($report=$result->fetch_assoc()) 
        {                                 
        echo "<tr>";  
        echo "<td>".$report['name']."</td>"; 
        echo "<td>".$report['leavetype']."</td>";
        echo "<td>".$report['fromdate']."</td>";
        echo "<td>".$report['todate']."</td>";
        echo "<td>".$report['supervisor']."</td>";
        echo "<td>".$report['reason']."</td>";
        echo "<td>".$report['Days']."</td>";
        echo "<td id='status$i'>pending</td>";
        echo "<td>" . '<button type="button" name="approve"'.$i.' onClick="return function_name($i,approve);">Approve</button>' . "</td>";
        echo "<td>" . '<button type="button" name="reject"'.$i.' onClick="return function_name($i,reject);">Reject</button>' . "</td>";  
        $i++;                              
        }
    ?>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

Not working. One addition column shows with predefined value of 'pending' But nothing happens after clicking Accept/Reject button
where does the 'status_id' gets its value from ?
Its a dynamic creation of status_id... check code in details... : <td id='status$i'>pending</td>... After that if you don't get it... then i sure you are not doing any searching...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.