想通过AJAX改变状态

时间:2016-06-13 09:59:16

标签: php jquery ajax

实际上按下按钮,我想调用该函数,并通过这个我想更新db上的一些数据。

这是我的代码:

echo "<button type='button' class='btn btn-info btn-md' id='click' onclick='loadDoc()'>Approve</button>"; 
// as the button is click loadDoc() is call

loadDoc()代码在这里:

function loadDoc() 
{
     var xhttp = new XMLHttpRequest();  
     xhttp.open('GET', 'updatebox.php', true);
     xhttp.send();
     document.getElementById('demo').innerHTML = 'hello';
}

这是我的updatebox.php

<?php
   include_once("Order.php");
   include("connection.php");
   $query="UPDATE orders
   SET status='cooking'
     WHERE order_id='this.order_id'";
   $filter_Result=mysqli_query($connection,$query);

  // here is the problem i just want to update some data into my table and   after that i want to remove the button 

?>

first pic

second pic

1 个答案:

答案 0 :(得分:1)

要更新表格中的数据,您必须使用update查询,请查看 SQL Update 。检查数据库是否更新使用条件如下例所示:

if (mysqli_query($connection,$query)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

然后隐藏按钮,您可以使用get()请求:

$.get('updatebox.php',{},function(){
     $('click').hide();
})

OR与XMLHttpRequest成功回调:

xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        document.getElementById("click").style.display = 'none';
    }
};

希望这有帮助。

相关问题