Using variable on php to create another variable in JS
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.
javascript php jquery database
add a comment |
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.
javascript php jquery database
Why don't you tryecho
for that?
– Nico Haase
Nov 23 '18 at 7:12
1
Possible duplicate of How to pass variables and data from PHP to JavaScript?
– yivi
Nov 24 '18 at 10:27
add a comment |
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.
javascript php jquery database
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.
javascript php jquery database
javascript php jquery database
edited Nov 23 '18 at 6:31
Julian Stark
1,3011527
1,3011527
asked Nov 23 '18 at 5:29
Gerald ManibaleGerald Manibale
347
347
Why don't you tryecho
for that?
– Nico Haase
Nov 23 '18 at 7:12
1
Possible duplicate of How to pass variables and data from PHP to JavaScript?
– yivi
Nov 24 '18 at 10:27
add a comment |
Why don't you tryecho
for that?
– Nico Haase
Nov 23 '18 at 7:12
1
Possible duplicate of How to pass variables and data from PHP to JavaScript?
– yivi
Nov 24 '18 at 10:27
Why don't you try
echo
for that?– Nico Haase
Nov 23 '18 at 7:12
Why don't you try
echo
for that?– Nico Haase
Nov 23 '18 at 7:12
1
1
Possible duplicate of How to pass variables and data from PHP to JavaScript?
– yivi
Nov 24 '18 at 10:27
Possible duplicate of How to pass variables and data from PHP to JavaScript?
– yivi
Nov 24 '18 at 10:27
add a comment |
4 Answers
4
active
oldest
votes
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
I don't understand but thankyou
– Gerald Manibale
Nov 23 '18 at 6:00
add a comment |
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'] ?>`;
add a comment |
===============
// 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.
add a comment |
- you could try giving an id or class to the status.
- 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);
}
}
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
– Gerald Manibale
Nov 23 '18 at 6:27
replacesetInterval
withsetTimeout
– marvinIsSacul
Nov 23 '18 at 7:10
If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
– Gerald Manibale
Nov 23 '18 at 7:21
i've modified my answer. check it out.
– marvinIsSacul
Nov 23 '18 at 7:25
I don't understand this. I want it to notify me when the status is "Out".
– Gerald Manibale
Nov 23 '18 at 8:01
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441073%2fusing-variable-on-php-to-create-another-variable-in-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
I don't understand but thankyou
– Gerald Manibale
Nov 23 '18 at 6:00
add a comment |
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
I don't understand but thankyou
– Gerald Manibale
Nov 23 '18 at 6:00
add a comment |
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
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
answered Nov 23 '18 at 5:34
FrankerZFrankerZ
17.8k73066
17.8k73066
I don't understand but thankyou
– Gerald Manibale
Nov 23 '18 at 6:00
add a comment |
I don't understand but thankyou
– Gerald Manibale
Nov 23 '18 at 6:00
I don't understand but thankyou
– Gerald Manibale
Nov 23 '18 at 6:00
I don't understand but thankyou
– Gerald Manibale
Nov 23 '18 at 6:00
add a comment |
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'] ?>`;
add a comment |
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'] ?>`;
add a comment |
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'] ?>`;
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'] ?>`;
edited Nov 25 '18 at 18:28
FrankerZ
17.8k73066
17.8k73066
answered Nov 23 '18 at 6:19
AkhileshAkhilesh
626
626
add a comment |
add a comment |
===============
// 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.
add a comment |
===============
// 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.
add a comment |
===============
// 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.
===============
// 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.
answered Nov 23 '18 at 6:30
new_usernew_user
1,3451018
1,3451018
add a comment |
add a comment |
- you could try giving an id or class to the status.
- 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);
}
}
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
– Gerald Manibale
Nov 23 '18 at 6:27
replacesetInterval
withsetTimeout
– marvinIsSacul
Nov 23 '18 at 7:10
If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
– Gerald Manibale
Nov 23 '18 at 7:21
i've modified my answer. check it out.
– marvinIsSacul
Nov 23 '18 at 7:25
I don't understand this. I want it to notify me when the status is "Out".
– Gerald Manibale
Nov 23 '18 at 8:01
|
show 1 more comment
- you could try giving an id or class to the status.
- 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);
}
}
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
– Gerald Manibale
Nov 23 '18 at 6:27
replacesetInterval
withsetTimeout
– marvinIsSacul
Nov 23 '18 at 7:10
If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
– Gerald Manibale
Nov 23 '18 at 7:21
i've modified my answer. check it out.
– marvinIsSacul
Nov 23 '18 at 7:25
I don't understand this. I want it to notify me when the status is "Out".
– Gerald Manibale
Nov 23 '18 at 8:01
|
show 1 more comment
- you could try giving an id or class to the status.
- 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);
}
}
- you could try giving an id or class to the status.
- 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);
}
}
edited Nov 23 '18 at 8:50
answered Nov 23 '18 at 6:13
marvinIsSaculmarvinIsSacul
53718
53718
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
– Gerald Manibale
Nov 23 '18 at 6:27
replacesetInterval
withsetTimeout
– marvinIsSacul
Nov 23 '18 at 7:10
If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
– Gerald Manibale
Nov 23 '18 at 7:21
i've modified my answer. check it out.
– marvinIsSacul
Nov 23 '18 at 7:25
I don't understand this. I want it to notify me when the status is "Out".
– Gerald Manibale
Nov 23 '18 at 8:01
|
show 1 more comment
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
– Gerald Manibale
Nov 23 '18 at 6:27
replacesetInterval
withsetTimeout
– marvinIsSacul
Nov 23 '18 at 7:10
If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
– Gerald Manibale
Nov 23 '18 at 7:21
i've modified my answer. check it out.
– marvinIsSacul
Nov 23 '18 at 7:25
I don't understand this. I want it to notify me when the status is "Out".
– Gerald Manibale
Nov 23 '18 at 8:01
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
– Gerald Manibale
Nov 23 '18 at 6:27
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
– Gerald Manibale
Nov 23 '18 at 6:27
replace
setInterval
with setTimeout
– marvinIsSacul
Nov 23 '18 at 7:10
replace
setInterval
with setTimeout
– marvinIsSacul
Nov 23 '18 at 7:10
If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
– Gerald Manibale
Nov 23 '18 at 7:21
If I use setTimeout, it will not automatically refresh my web. I needed that to get the latest value from my database
– Gerald Manibale
Nov 23 '18 at 7:21
i've modified my answer. check it out.
– marvinIsSacul
Nov 23 '18 at 7:25
i've modified my answer. check it out.
– marvinIsSacul
Nov 23 '18 at 7:25
I don't understand this. I want it to notify me when the status is "Out".
– Gerald Manibale
Nov 23 '18 at 8:01
I don't understand this. I want it to notify me when the status is "Out".
– Gerald Manibale
Nov 23 '18 at 8:01
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441073%2fusing-variable-on-php-to-create-another-variable-in-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why don't you try
echo
for that?– Nico Haase
Nov 23 '18 at 7:12
1
Possible duplicate of How to pass variables and data from PHP to JavaScript?
– yivi
Nov 24 '18 at 10:27