Good day, experts! =)

In general, I looked at a bunch of materials about the transfer of a variable from js / jqwery to PHP, all of these materials explained that this is not a trivial task, a bunch of lines of code were cited as an example, for example (some wild methods, libraries lines 25-100 of the code) ...

And today, at 3:30 in the morning, sitting at the solution of the next problem, a related issue, and after drinking a couple of bottles of delicious beer, it “dawned on me” -

Why write all this unnecessary code from the examples, when everything can be done so simply that I did not believe myself that it would work at all. In short, the doubt has crept in that it will work out at all, but as it turned out, everything ingenious is simple ... Here is the code for the actual thing that occurred to me:

<script> var peremennayaJs= 1; <?$peremennayaPhp="peremennayaJs";?>; alert(<?echo $peremennayaPhp;?>); </script> 

I was very surprised that it worked, initially skeptical that it was coming in a drunken stupor, but no ...

That doubt has crept in, maybe I missed something, I can not do it for some unclear reasons. Simply, I program I can say the first month, and then only procedural programming in PHP, c js just starting to look at the simplest things.

I ask you not to throw stones if this is trivial, and I now share what everyone knows as 2 + 2 ... Just then why are you misleading that the transfer of the value of the variable js to php is PPC as difficult ?!

In general, the question is how permissible? Is it possible to use it, are there any pitfalls?

  • Kostyan, you have var peremennayaJs=1 at the exit to the alert and not with php. Now I will write in the answer in detail why this is happening - Mr. Black
  • I check in Denver. I get an alert kokraz unit (in Google Chrome and firefox). The point is not that the alert is dropped, but because the value is passed to the variable php, isn't it ?! - Konstantin
  • To transfer variables, you need to use queries or leave values ​​in the tag attributes - Mr. Black
  • Not holivara sake, IMHO, use Denver in year 16, this is a moveton. - Vladimir Gamalyan
  • I am amazed by the guys, who minus the question if it seems simple and obvious to them. The question is completely decomposed and described in detail even with a surplus - Mr. Black

1 answer 1

So in the html document we have the code

 <script> var peremennayaJs = 1; <?php $peremennayaPhp = "peremennayaJs"; ?> alert(<?php echo $peremennayaPhp; ?>); </script> 

After building a document, $peremennayaPhp carries the (string) "peremennayaJs" , and not the value from var peremennayaJs . As a result, this alert(peremennayaJs) function alert(peremennayaJs) comes out, it outputs 1

  • Yes! I gave it to Makha, I hurried to rejoice ... Only I realized that I finally did ... - Konstantin
  • @ Konstantin, anything can happen :) - Mr. Black
  • Good. From here a question - is there a method in js like echo in php? Ie- $ peremennayaPhp = <script> "echo" peremennayaJs </ script> - Konstantin
  • @Konstantin, for transfer to the server without requests in any way - Mr. Black
  • @ Konstantin, I got a function in js for 16 lines, which sends requests with callbacks and makes it possible to send at least dozens of requests at the same time with just one line. What problems to write such a function yourself? - Mr. Black