-1

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",
4
  • 3
    Possible duplicate of How to embed php in javascript? Commented Apr 16, 2017 at 10:57
  • 1
    it would work! youre just missing a , behind your value data... Commented Apr 16, 2017 at 10:59
  • Try this, value: '<?php echo 'Hello World!' ?>', Commented 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. Commented Jul 28, 2024 at 13:30

2 Answers 2

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",
Sign up to request clarification or add additional context in comments.

1 Comment

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?> '.
1

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

var example ="<?php echo date();?>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.