2017-01-18 86 views
0

我正在使用PhP-Mysql製作一個休假管理系統。按鈕點擊修改表格列數據

我有一張表,它在申請葉子時從用戶處獲得輸入。 (名稱,leavetype,fromdate,todate,主管,原因和狀態)。只有狀態欄有一個預定義的值'pending'。

現在我想在每一行中引入兩個按鈕(Accept/Reject)。點擊後將更改「狀態」字段的值。

我不知道該怎麼做,我試過更新表列,但只有在有條件時纔會更新,這對於這種情況不會是正確的過程。

<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
//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> 
+0

不工作。一個附加列顯示預定義的'pending'值但點擊Accept/Reject按鈕後沒有任何反應 –

+0

'status_id'從哪裏得到它的值? –

+0

它的一個動態創建status_id ...檢查代碼的詳細信息...: pending ...之後,如果你沒有得到它......那麼我確定你是不做任何搜索... – pAsh