-1

I'm having a hard time getting the value of a specific variable in php to use in js. This is my code in php:

<?php
require("connection.php");

$sql_cmd = "SELECT * FROM tbstatus";

$stmt = $con->prepare($sql_cmd);
$stmt->execute();

echo "<h2>STATUS OF THE BABY</h2>";

while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<h4>" . $result['status'] . "</h4>";
}
?>

I want to get the value of this ($result['status']) and pass it on the variable pos in js. This is my js code:

setInterval(function() {
    $("#position").load('refresh.php');
    notif();

}, 1000);

function notif() {
    var pos = $('PHP VARIABLE HERE').val();
    alert(pos);
}

Thanks for your help.

julianstark999
  • 3,450
  • 1
  • 27
  • 41
  • Why don't you try `echo` for that? – Nico Haase Nov 23 '18 at 07:12
  • 1
    Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – yivi Nov 24 '18 at 10:27

4 Answers4

1

The easiest way is to output it to javascript directly:

?>
<script type="text/javascript">
window.MY_PHP_VAR = <?php echo json_encode($myPhpVar); ?>;
</script>
...

window.MY_PHP_VAR now contains your php variable
Blue
  • 22,608
  • 7
  • 62
  • 92
1

if your javascript code is on same page where the result is comming then you can use this

var pos =  `<?php echo $result['status'] ?>`;

var pos =  `<?= $result['status'] ?>`;
Blue
  • 22,608
  • 7
  • 62
  • 92
Akhilesh
  • 927
  • 1
  • 6
  • 21
-1
  1. you could try giving an id or class to the status.
  2. then in JavaScript you could then get the value of the id or class.

PHP

<?php
require("connection.php");

$sql_cmd = "SELECT * FROM tbstatus";

$stmt = $con->prepare($sql_cmd);
$stmt->execute();

echo "<h2>STATUS OF THE BABY</h2>";
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<h4> <span class="status">' . $result['status'] . '</span></h4>';
}

?>

JavaScript

var oldStatus = '';

setInterval(function() {
    $("#position").load('refresh.php');
    notif();
}, 1000);

function notif() {
    // note that since we used a class, you will get the value of the first element only.
    var pos = $('.status').text(); // use .text() instead of .val()
    if (pos.toLowerCase() == 'out' && pos != oldStatus){
        oldStatus = pos;
        alert(pos);
    }
}
-1
===============
// refresh.php
===============

<?php  
require("connection.php");
$sql_cmd = "SELECT * FROM tbstatus";
$stmt = $con->prepare($sql_cmd);
$stmt->execute();

echo "<h2>STATUS OF THE BABY</h2>";

while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
      echo "<h4>" . $result['status'] . "</h4>";
      echo "<script> alert('". $result['status'] ."'); </script>";
      /* If the $result['status'] is 'success' the above line will be converted to:
       echo "<script> alert('success'); </script>";
      */
}

?>

so, every time the refresh.php loads, the script is going to get executed.

However, I suggest you to assign a id or class attribute to your h4 where you are echoing your status and access the value using the selectors in the javascript.

Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34