Dhruv Tyagi Dhruv Tyagi - 5 months ago 8
Ajax Question

want to change the state through AJAX

Actually as the button press, I want to call the function and through this I want to update some Data on db.

Here is my Code :

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()
code is here :


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


Here is my
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

?>

Answer

To update the data in your table you have to use update query, take a look to SQL Update. to check if the db is updated use condition like example below :

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

Then to hide the button you could use get() request instead :

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

OR with XMLHttpRequest success callback :

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

Hope this helps.