Ben Za Ben Za - 9 months ago 35
PHP Question

Ajax - Check for database update

Im making a autobuy system. I have a invoice page that show with default - payment processing. I have made it so that when they actually buy via coinpayments.net, it updates the database to status 1. I want to make it so that when the status changes to 1 in the database, it updates the invoice page without refreshing and says "Payment Status: Completed". I have some ajax and php code but it doesn't seem to be working. I update the database status to 1 and no text changes on my page. I have a timer interval that should be updating every 2 seconds but something is wrong. Please help.

My HTML status code

<input type="hidden" id="order_numb" value="<?php echo $_SESSION['succ_itm_id']; ?>">

<div id="payment" style="text-align: center;">
Status<br><img src="../img/processing/payment_proc.gif"><br>Payment Pending
</div>


My AJAX code

<script>
function send_data() {
var order_id = $('#order_numb').val();
$.ajax ({
type: "POST",
url: '../rld_payment.php',
data: { order_num: order_id },
success:function(data)
{
$('#payment').innerHTML = 'Status<br><b>Payment Completed!</b><br><b>Redirecting</b> ...';
}
});
}

send_data();
setInterval(send_data, (2 * 1000));
</script>


My PHP code to check status

<?php
include('conf/db_.php');

$order_id = mysqli_real_escape_string($con, $_POST['order_num']);
$order_status = mysqli_query($con, "SELECT `status` FROM `orderinfo` WHERE `order_id` = '$order_id'") or die(mysqli_error($con));
if($order_status == 1)
{
echo 'Status<br><b>Payment Completed!</b><br><b>Redirecting</b> ...';
return true;
}

?>

Answer Source

Why do you need to store session variable in a hidden field if you have it already stored in a session that can be easily accessed on any other page? Unless you have a valid reason for doing so, just use $_SESSION['succ_itm_id'] in your payment script.

Second, your query only selects the record NOT updates the record.

I would simply do,

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include 'conf/db_.php';

$order_id = $_SESSION['succ_itm_id'];
$result = mysqli_query($con, "UPDATE orderinfo SET status = 1 WHERE order_id = $order_id");
if ($result) {
    echo 'Payment Status: Completed';
}
?>

In your JavaScript code, you would then not need to pass anything.

Note that the response (what you echo'd in your payment script) is stored in the data variable, so you can use it to add in the HTML.

Note also that $(selector) is always a jQuery object, not a native JavaScript element. A jQuery object doesn't have a property innerHTML but has a convenient .html() method.

<script>
function send_data() {
    /*$.ajax({
        type: 'POST',
        url: '../rld_payment.php',
        success: function (data) {
            $('#payment').html(data);
        }
    });*/

    console.log('sending data');
    // shorthand method to $.ajax()
    $.post('../rld_payment.php', function (data) {
        console.log('receiving data', data);
        $('#payment').html(data);
    });
}

send_data();
setInterval(send_data, 2 * 1000);
</script>