Hello
I am trying to pass data from php to js and return the values like in the code. The problem I am facing is where I am testing I have hardcoded to values into a string in js and the code works fine. What I need to do however, is grab these values from php. I have tried various options such as var str = data; but it is not grabbing any data. I would be grateful if someone could point out my error. thanks
loadActions.php
<?php
$brtvsql = "SELECT * FROM act WHERE activity='Box Retrieval' AND new = '1'";
$result = mysqli_query($conn, $brtvsql);
$brtv_row = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);
echo 'brtv' . ' ' . $count;
$brtnsql = "SELECT * FROM act WHERE activity='Box Return' AND new = '1'";
$result = mysqli_query($conn, $brtnsql);
$brtn_row = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);
echo 'brtn' . ' ' . $count;
?>
js code
setInterval(function() {
$.get('/domain/admin/loadActions.php', function(data) {
var str = "brtv 30;brtn 0";
console.log(data);
var data = {};
str.split(";").forEach(function(s) {
data[s.split(" ")[0]] = s.split(" ")[1];
});
// Check the result
if(data["brtv"]) {
console.info("brtv: %s", data["brtv"]);
$(".retrievals").text(data["brtv"]);
}
if(data["brtn"]) {
console.info("brtn: %s", data["brtn"]);
$(".returns").text(data["brtn"]);
}
});
}, 15000);