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.
from some reason i have to send value from js to php and i try this basic code and it doesn't work .
<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>";
NOTE THAT js_var shoul be a global variable, not local one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.