Using variable on php to create another variable in JS

how to convert javascript variable to php variable
passing javascript variables to php
pass php variable to javascript function
pass php variable to javascript file
how to assign javascript variable to php variable
how to store javascript value in php variable
how to get javascript variable value in php in same page
post javascript variable to php

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.

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

How to create Javascript variables from PHP variables?, nice article, but this will only work if we are using JS in the same PHP file. What if we include the js from some other file? Favorite heart outline  Echo the Data and Use JavaScript to Get the Information from the DOM. This method is not better than AJAX, but it has its advantages. It's still relatively separated between PHP and JavaScript in a way that there is no PHP right in the JavaScript. This will create an element which will not be shown to the user but is recognizable to JavaScript. index.php

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'] ?>`;

Passing PHP Variables to JavaScript, We demonstrate with string, boolean, and numeric values (i.e., scalar values). To pass scalar data held in a PHP variable ( $val ) to a JavaScript variable, are valid in PHP may generate errors in JavaScript when output using this approach. JavaScript is the client side and PHP is the server side script language. The way to pass a JavaScript variable to PHP is through a request. Method 1: This example uses form element and GET/POST method to pass JavaScript variables to PHP. The form of contents can be accessed through the GET and POST actions in PHP.

===============
// 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.

Exchanging variables between JavaScript and PHP, And conversely, how to use the values ​​of PHP variables in JavaScript? form​, and has the only purpose add a value to other values provided by the form. The really short answer to both your questions is that when you make one variable equal to another, a COPY of what's in the first variable is made and stored in the second variable - there is no linkage between the two variables. But, read on for more details and why it can seem like there is a link in some cases

  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);
    }
}

Variable variables - Manual, A normal variable is set with a statement such as: Further dereferencing a variable property that is an array has different semantics between PHP 5 and PHP 7. If you want to use a js variable in a php script you MUST pass it within a HTTP request. There are basically two ways: Submitting or reloading the page (as per Chris answer). Using AJAX, which is made exactly for communicating between a web page (js) and the server(php) without reloading/changing the page. A basic example can be:

How to pass JavaScript variables to PHP ?, The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the While creating a cookie, an expire time should also be specified, which is 10 days for this case. How to pass form variables from one page to other page in PHP ? I'm trying to include JavaScript variables into PHP code as PHP variables, but I'm having problems doing so. When a button is clicked, the following function is called: &lt;script type="text/javas

How to pass variables and data from PHP to JavaScript , In this article, let's see how to pass data and variables from PHP to JavaScript. First, we can pass the data using the simple assignment operator if we want to perform the operation How to declare variables in different ways in JavaScript? Notice the quotes around the PHP tags. This will result in a string value in JavaScript which you may need to convert for use in your scripts. If the PHP value is numeric, you don't need to include the quotes. Example PHP Variables to JavaScript. Here we demonstrate with boolean, numeric, and string values assigned to PHP variables:

PHP Variables, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Creating (​Declaring) PHP Variables. In PHP, a variable starts with the $ sign, followed by the name of the variable: Note: Unlike other programming languages, PHP has no command for  In programming, just like in algebra, we use variables (like price1) to hold values. In programming, just like in algebra, we use variables in expressions (total = price1 + price2). From the example above, you can calculate the total to be 11.

Comments
  • Why don't you try echo for that?
  • Possible duplicate of How to pass variables and data from PHP to JavaScript?
  • It's working now. Thank You. But I have a little bit concern. Since I have setInterval in my js, every second it will alert me with the value of status. I want is to just alert me once, if the value of the status does not change. Is it possible? Thank you
  • replace setInterval with setTimeout
  • If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
  • i've modified my answer. check it out.
  • I don't understand this. I want it to notify me when the status is "Out".