[英]pass javascript value to php in exact page without refresh
i'm new to this so i hope you could help me. 我是新来的,所以我希望你能帮助我。
i'm try to build one page has the ability to add and remove from sqldb. 我试图建立一个具有添加和删除sqldb功能的页面。
from some reason i have to send value from js to php and i try this basic code and it doesn't work . 由于某种原因,我必须将值从js发送到php,我尝试了这个基本的代码,它无效。
<html>
<head>
<script src="resources/js/jquery-2.2.3.js"> </script>
</head>
<body>
<form method="get">
<input type="button" value="submit" name="submit" class="test">
</form>
<div id="state"></div>
</body>
<script type="text/javascript">
$('.test').click(passValue);
function passValue(e){
document.getElementById('state').innerHTML="button is clicked";
var js_var = "xml recevid";
var xhr = new XMLHttpRequest();
var url = 'test.php?js_var=' + js_var;
xhr.open('GET', url, false);
xhr.onreadystatechange = function () {
if (xhr.readyState===4 && xhr.status===200) {
document.getElementById('state')xhr.responseText;
}
}
xhr.send();
}
</script>
<?php
if (isset($_GET['js_var'])) $php_var = $_GET['js_var'];
else $php_var = "<br />js_var is not set!";
echo $php_var;
?>
USE $php_var ="<script type='text/javascript'>document.write('js_var');</script>";
USE
$php_var ="<script type='text/javascript'>document.write('js_var');</script>";
NOTE THAT js_var shoul be a global variable, not local one. 注意js_var应该是全局变量,而不是局部变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.