2017-09-11 34 views
0

怎麼每次我點擊相應按鈕 我的繼承人對錶單代碼時得到行的值AJAX PHP的GET行值

 <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post"> 
      <tbody> 
       <?php 
       while ($reserve=mysqli_fetch_array($record)) { 
       echo "<tr>"; 
       echo "<td>".$reserve['id']."</td>"; 
       echo "<td>".$reserve['room']."</td>"; 
       echo "<td>".$reserve['status']."</td>"; 
       echo "<td>".$reserve['client']."</td>"; 
       echo "<td>".$reserve['dateandtime']."</td>"; 
       echo "<td>₱ ".$reserve['balance']."</td>"; 
       echo "<td>".$reserve['additional']."</td>"; 
       ?> 
        <td style="text-align: center;"> 
        <div class="ui buttons"> 
         <button type="submit" name="approved" class="Huge ui green button" id="update">Approved</button> 
         <button type="submit" name="cancel" class="Huge ui red button">Cancel</button> 
         <button type="submit" name="checkin" class="Huge ui teal button">Check In</button> 
         <button type="submit" name="checkout" class="Huge ui violet button">Check Out</button> 
        </div> 
        </td> 
       </tr> 
      <?php } ?> 
      </tbody> 

我想要的是讓每次行的值我點擊了記者按鈕,這樣我可以更新

回答

2

試試這個:添加屬性reserveId的更新按鈕,使用jQuery腳本來獲取標識按照當前行的時候,你會點擊更新按鈕。

 <tbody> 
      <?php 
      while ($reserve=mysqli_fetch_array($record)) { 
      echo "<tr>"; 
      echo "<td>".$reserve['id']."</td>"; 
      echo "<td>".$reserve['room']."</td>"; 
      echo "<td>".$reserve['status']."</td>"; 
      echo "<td>".$reserve['client']."</td>"; 
      echo "<td>".$reserve['dateandtime']."</td>"; 
      echo "<td>₱ ".$reserve['balance']."</td>"; 
      echo "<td>".$reserve['additional']."</td>"; 
      ?> 
       <td style="text-align: center;"> 
       <div class="ui buttons"> 
        <button type="submit" reserveId="<?=$reserve['id']?>" name="approved" class="Huge ui green button" id="update">Approved</button> 
        <button type="submit" name="cancel" class="Huge ui red button">Cancel</button> 
        <button type="submit" name="checkin" class="Huge ui teal button">Check In</button> 
        <button type="submit" name="checkout" class="Huge ui violet button">Check Out</button> 
       </div> 
       </td> 
      </tr> 
     <?php } ?> 
     </tbody> 


<script type="text/javascript"> 
    $(document).on('click', "#update", function(e){ 
     e.preventDefault(); 

     var reserve_id = $(this).attr('reserveId'); 
     alert('Your reserve_id is: '+ reserve_id); 

     jQuery.ajax({ 
      type : "post", 
      dataType : "json", 
      url : 'your-file.php', 
      statusCode: { 
       500: function() { 
       alert(" 500 data still loading"); 
       console.log('500 '); 
      } 
     }, 

     data : { reserve_id : reserve_id, action: 'update'}, 

     error: function(xhr, status, error) { 
      var err = eval("(" + xhr.responseText + ")"); 
      alert(err.Message); 
     }, 
     success: function(response) { 
      alert(response.res_message);  
     }, 
     }); 
    }); 
</script> 

您-file.php

<?php 
    if (isset($_POST['action'])) { 

     if ($_POST['action'] == 'update') 
     { 
      $reserve_id = $_POST['reserve_id']; 
      #update code here 

      $data['res_message'] == "Record updated for the id: $reserve_id"; 
     } 
     else 
     { 
      #code for another action for example delete 
      $data['res_message'] == "Invalid request"; 
     } 

     echo json_encode($data); die(); 

    } 
    else 
    { 
     $data['res_message'] == "Invalid action request"; 
     echo json_encode($data); die(); 
    } 
?> 
+0

由於這是工作,但我怎麼能叫這在PHP? $ ID = $ _POST [ 'ID']; ? – Hysteria

+0

嘿@Hysteria,我已經更新了PHP腳本的代碼,我已經使用jQuery的阿賈克斯。看看這可以幫助你:) – Harish