【问题标题】:How to update database table and change the button using ajax php?如何使用 ajax php 更新数据库表和更改按钮?
【发布时间】:2017-12-05 19:33:02
【问题描述】:

我的页面中有一个名为“完成付款”的按钮:

<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";
}

【问题讨论】:

  • 那么什么不起作用?未更新 db 值或未更改按钮文本?
  • 您是否遇到任何错误?检查您的浏览器控制台。
  • 没有任何错误...

标签: php jquery ajax


【解决方案1】:

像这样更改您的更新查询:

$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.
        }  
   });

【讨论】:

  • 好的,那么问题是什么?你能回复我的评论_那么什么不起作用?没有更新 db 值或没有更改按钮文本?_
  • 我只需要更新数据库和支付按钮文本...就是这样..
  • 数据库没有被改变?你有什么错误吗?
  • 请查看代码的 ajax 部分...我想我错过了一些东西...
  • 他也在 url 中传递id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2018-02-14
  • 1970-01-01
  • 2019-08-15
  • 2011-07-10
  • 2016-09-19
相关资源
最近更新 更多