My coding isn't working. Please anyone could tell me why? Here's the code:
AJAX code:
function sale(sale_code,sale_quantity) {
if (sale_code.length == 0 || sale_quantity == 0) {
alert("Product's code & quantity are required!");
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("show").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "sl.php?sdoc_no=<?= $sdoc_no ?>&sdoc_date=<?= $sdoc_date ?>&sale_code=" + sale_code + "&sale_quantity=" + sale_quantity, true);
xmlhttp.send();
}
}
HTML & PHP:
<?php $qitem = mysqli_query($conn, "SELECT * FROM stocks WHERE stock_type = 'product'");
while($ritem = mysqli_fetch_array($qitem)){ ?>
<div class="col-md-4 col-sm-6 text-center">
<button class="btn btn-info btn-block" type="button" onclick="sale(<?= $ritem['stock_code'] ?>, 1)"><?= $ritem["stock_name"] ?><br />
<small>RM <?= number_format($ritem["stock_sprice"], 2) ?></small>
</button>
</div>
<?php } ?>
sl.php file is working and returning data if i'm put it manually. And take note that I'm using bootstrap tab for this button.
$ritem['stock_code']contain? Text or just numbers?sale('<?= $ritem['stock_code'] ?>', 1). (Add'-signs around the parameter, which is needed for strings). You should also uri encode your parameters before adding them as querystring, For js: encodeURI and for PHP: urlencode