I'm trying to define a value by PHP echo with data received from MySQL.
How to put PHP echo code into JavaScript?
Example:
document.addEventListener("DOMContentLoaded", function(event) {
var gg1 = new JustGage({
id: "gg1",
value: <?php echo 'Hello World!' ?>
min: 0,
max: 100,
title: "Target",
-
3Possible duplicate of How to embed php in javascript?Marcin– Marcin2017年04月16日 10:57:53 +00:00Commented Apr 16, 2017 at 10:57
-
1it would work! youre just missing a , behind your value data...Jonas Wilms– Jonas Wilms2017年04月16日 10:59:15 +00:00Commented Apr 16, 2017 at 10:59
-
Try this, value: '<?php echo 'Hello World!' ?>',manian– manian2017年04月16日 11:01:40 +00:00Commented Apr 16, 2017 at 11:01
-
This question is similar to: How to incorporate a PHP-generated value into JavaScript code on the page?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.dumbass– dumbass2024年07月28日 13:30:52 +00:00Commented Jul 28, 2024 at 13:30
2 Answers 2
You just missed the quotation around the <?php & semi-colon before ?>,
document.addEventListener("DOMContentLoaded", function(event) {
var gg1 = new JustGage({
id: "gg1",
value: "<?php echo 'Hello World!'; ?>", // This line is edited
min: 0,
max: 100,
title: "Target",
Sign up to request clarification or add additional context in comments.
1 Comment
dale landry
Also missed basic syntax like the semi-colon to end your echo string,
'Hello World!' should be 'Hello World!'; If you have it turned on in your php.ini file, you can also use ' <?=$variable?> '.In order to use Php varibale inside Javascript or JQuery call them like this..
var example ="<?php echo date();?>";
answered Apr 16, 2017 at 11:06
Anuj Tiwary
3381 silver badge10 bronze badges
Comments
default