Hello. Such a problem: learned the initial course javascript and jquery. Now with writing a script, exporting images, I ran into a problem. The fact is that my project is on WordPress and I need to have a unique “static” variable in each of my posts. That is, for example, there is a record 1 and in the code for this record there is a unique variable that never changes, as well for records 2,3,4. Maybe for this you need to save the variable via php to the database, and then output it through JavaScript and work with it? Tell me, I do not know. Thank.

Just ask, can anyone tell me how to "bind" two languages ​​of Java Script and PHP?

    3 answers 3

    actually and what prevents to use standard record ID ??? It is unique for each page ... and in Js code you can simply transfer it ...

    <script> var a = <?php global $post; echo $post->ID; ?> </script> 

    but in this case this script should be in the header ... and not in a separate JS file ... which is not good ...

      You can pass variables in the address:

       http://www.somesite.com/script.js?a=5&b=6 http://www.somesite.com/script.php?a=5&b=6 

      only with Cyrillic there will be problems because the web is for some reason designed so that everyone uses DOS and only 256 characters are supported in the address

      UPD: In PHP, they will call this: $ _REQUEST ['variable name'] such a method is called POST; everything is transferred to it in the address bar. There is also a GET method, but you probably read about it yourself :) but in JavaScript you can simply access the variable by name


       define("pi",3.14); // Обьявляет константу pi=3.14 echo pi; // Выведет 3.14 pi=5; // pi так и останется 3.14 echo pi; // Выведет 3.14 

      A constant is a 'variable' that cannot be changed after an announcement

      • At the expense of communication, I understood everything, thanks. I still can not understand about the variable. Suppose there are 200 pages of the site, I need to create a DIFFERENT variable on EVERY page that NEVER CHANGED under any circumstances. Just was and in the right place I was displayed. For example, for page 1 there was a variable page1, for 2nd - page2. How to do this? - euqen
      • Pam will help constants. See UPD to my answer - Rules

      Well, you can associate the JS variable with the PHP variable, for example:

       <?php $a = 3; echo("<script>a=$a</script>") ?> 

      But in order to associate a PHP variable with a JS variable, you have to sweat ... but you can try using dynamic Javascript - AJAX.

      • one
        echo has been written without parentheses for a long time - johniek_comp
      • Tell me who knows - in which version of echo was written with brackets? I love the story just ...: D - Zowie
      • Wash In PHP3 ... - Rules