0

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",
Hossam
  • 1,126
  • 8
  • 19
Wanarka
  • 93
  • 6

2 Answers2

1

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",
Hossam
  • 1,126
  • 8
  • 19
  • 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?> '. – dale landry Apr 16 '17 at 11:33
1

In order to use Php varibale inside Javascript or JQuery call them like this..

var example ="<?php echo date();?>";
Anuj Tiwary
  • 318
  • 1
  • 10