如何使用ajax php更新数据库表并更改按钮?

时间:2017-12-05 11:10:18

标签: php jquery ajax

我的页面中有一个名为Finish Payment的按钮:

<button style="background-color:#73bd5a; color:#FFF;" id="finish_payment" 
name="finish_payment" type="button" class="btn btn-white active pull-right">
<i class="ion-checkmark-circled"></i></i> Finish Payment</button>

我想要做的是当我点击此按钮时,我想在表client_payment中将付款状态“未付”更新为“付费”,并在更新后将按钮文本更改为已付款而不刷新页面。我使用的ajax代码如下:

<script>
$(document).ready(function(){
         $(document).on('click', '.finish_payment', function(){  

       $.ajax({  
            url:"update_payment.php?id=<?php echo $client_id; ?>",  
            method:"POST",  
            dataType:"json",  
            success:function(data){  
              alert(data);  
            }  
       });  
  });  

我的服务器端代码是:

$connection = mysqli_connect("localhost", "root", "", "bn");
 $client_id=$_GET['id'];

 $query="update client_payment set payment_status='true' where 
  client_id=$client_id";
 if(mysqli_query($connection, $query)){

echo"Payment Complete";
}

1 个答案:

答案 0 :(得分:0)

更改您的更新查询,如下所示:

$query="update client_payment set payment_status='true' where 
  client_id='".$client_id."'";  // `'` was missing in where clause.

要更新按钮文字,您可以执行以下操作:

$(document).ready(function(){
     $(document).on('click', '#finish_payment', function(){  // finish_payment is id attribute not class. So using with #

     $.ajax({  
        url:"update_payment.php?id=<?php echo $client_id; ?>",  
        method:"POST",  
        dataType:"json",  
        success:function(data){  
            $('.finish_payment').text('Paid');    // Changing button text here.
        }  
   });