1

I need to know how i get my PHP variable into JavaScript as a text. I will show you my code:

     <script type="text/javascript">

        $('#krasvak').wScratchPad({
          scratchMove: function (e, percent) {
            console.log(percent);
            if (percent > 70)
            {
                this.clear();
                window.alert("U heeft uw code gekrast");
                window.location.href='compleet.php';        <!-- locatie waar je heen gaat na het krassen-->
            }
          }
        });
        $('#krasvak').wScratchPad('bg', 'HERE MUST THE VARIABLE COME ');        <!-- de onderste laag die vrijkomt-->
        $('#krasvak').wScratchPad('fg', 'images/overlay.png');      <!-- de laag die je weg krast-->
        $('#krasvak').wScratchPad('size', 15);
        $('#krasvak').wScratchPad('cursor', 'url("./images/coin.png") 5 5, default'); <!-- de muis in het krasgebied -->

      </script>
  <?php 

  $_COOKIE["number"];

  if ($_COOKIE["number"] < 5000)
  {
    $achtergrond = 'images/slide1.png';
  }
  else
  {
    $achtergrond = 'images/logo.jpg';
  }

  ?>

It is about the bg that is not empty.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bramos
  • 69
  • 9
  • 1
    show where you want to get php variable – Haseeb Sep 09 '14 at 08:00
  • 2
    Don't those HTML comments result in JavaScript syntax errors? Use JavaScript commenting instead here. – halfer Sep 09 '14 at 08:00
  • $('#krasvak').wScratchpad('BG', 'variable'); – Bramos Sep 09 '14 at 08:06
  • 1
    Hi OP. I've removed the 'cookies' tag, since as far as I can see the question is not actually about cookies. Similarly, we have a long-standing convention here that 'thanks in advance' and 'please help' are a bit too chatty, and are generally removed. Bear in mind that questions here are for posterity rather than just for each individual poster. – halfer Sep 09 '14 at 08:08

3 Answers3

1

You can simply echo your PHP variable into your JavaScript:

$('#krasvak').wScratchPad('bg', '<?php echo $achtergrond; ?>');

Of course, this requires your PHP run before your JavaScript: otherwise the PHP variable won't be set when this runs!

Meshaal
  • 694
  • 2
  • 7
  • 18
0

It is not a good practice to call PHP code in JS. It is hard to read an not a clean piece of code. But I know your problem. What you should do if you really need PHP in JS code is something like this:

<script type="text/javascript">
    // define your php to js variables:
    var php_background = "<?php echo json_encode($achtergrond); ?>";
    // more variables to go here...

    $('#krasvak').wScratchPad('bg', php_background);

</script>

With this you get an fast overview of all your variables you get from PHP to use them in JS.

David Leitner
  • 3,312
  • 1
  • 18
  • 27
  • 2
    You're missing quotes around the PHP tags. – halfer Sep 09 '14 at 08:09
  • I'm not convinced as to why it's not good practice. How is it any different than calling PHP code in HTML? Or, more pertinently, how would you even get around it? In this case you could check the cookie with JS, certainly, but there are plenty of cases where you need to access PHP data from within non-PHP code. – Meshaal Sep 09 '14 at 08:11
  • There is one BIG difference: HTML is a markup language and JS is a programming language. You should avoid mixing programming Languages in your Code because it is hard to maintain and to read. – David Leitner Sep 09 '14 at 08:50
0

Put your PHP code above the javascript. And use ur variable into javascript.

$('#krasvak').wScratchPad('bg', '<?php echo $achtergrond; ?>');
Jigisha Variya
  • 207
  • 1
  • 8