Transfer of variables from php to js and back without withdrawal
-
Good day.
- We have a code php in which
$x = 5
I need to give the meaning$x
In the JS code, which is written down on the page below, let's say compare it to the variable."qwe
" - We have a JS code in which
var qwe = 3
I need to give you the meaningqwe
"in the php code, which is written below on the page, say, add it to the variable.$x
♪
p.s. Alas and ah, there was a sea of such decisions in the interior, but with PHP-StateJQ-PHP.
- We have a code php in which
-
<html> <head> <script type="text/javascript"> var qwe = Math.random()*99+1; function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// Для свежих эксплореров и приличных браузеров xmlhttp=new XMLHttpRequest(); } else {// для старых эксплореров xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200)//Запрос обработан на стороне сервера и ждет обработки на клиенте * { qwe = xmlhttp.responseText;// присваиваем переменной qwe значение, которое выплюнул скрипт *** } } xmlhttp.open("GET","php_script.php?qwe="+qwe,true);//готовим ГЕТ запрос на наш пи-эйч-пи-шник. Заодно пересылаем нашу переменную на сервер. ** xmlhttp.send();//отправляем. } </script> </head> <body> <button type="button" onclick="loadXMLDoc()">Получить переменную</button> </body> </html>
PHP:
<?php
$x = rand(1,100);
$js_qwe = $_GET['qwe'];
print($x);
?>
Flight management.
*
- that's the answer from the server to our request.- here we send a variable qwe from JS-PHP.
We're doing PHP-StateJS.
P.S. W3Schools to you http://w3schools.com/ajax/default.asp ♪
P.P.S. Speaking of JS, which is down on the page.
<?php $x = rand(1,100); ?>
//....SOME_CODE
<script>
var qwe = <?=$x?>;
</script>
This number won't roll in paragraph 2. Only Ajax