Insert javascript value via php variable

Asked

Viewed 35 times

0

I need to insert value width and height through a php variable.

    window.addEventListener('DOMContentLoaded', function () {
            var image = document.querySelector('#image'); 
            var cropper = new Cropper(image, {
                ready: function () {
                    this.cropper.setCropBoxData({
      "left":750,
      "top":190,
      "width":500,
      "height":50
    });    
  },
});

Where :

"width":100,
"height":50

Would be for example:

"width": $VALOR,
"height":$VALOR2

But that way it doesn’t work,

Thank you in advance!

  • PHP <-> HTML, HTML <-> JS, JS x-x PHP !

  • https://answall.com/questions/51392/ler-os-dados-do-php-no-javascript

1 answer

0


If the file with your code is inside a tag <script> in a file with the extension . php, you can do so:

    window.addEventListener('DOMContentLoaded', function () {
            var image = document.querySelector('#image'); 
            var cropper = new Cropper(image, {
                ready: function () {
                    this.cropper.setCropBoxData({
      "left":750,
      "top":190,
      "width": <?= $width; ?>,
      "height": <?= $height; ?>
    });    
  },
});

If this javascript code is inside a file with the extension . js unfortunately has no way.

  • It worked as follows: <?= $value->width ? > Thanks!

  • then this variable $value was an object?

  • No, but I will use this way and create the Function to perform the query in the database, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.