Forgive the simplistic question. I'd like to pass a variable from javascript to php and I understand this can be done with an xmlhttprequest object. So I built a test script to figure out how to do this.
I'm using XAMPP and I placed the following files, main.php and test.php into their proper location in the htdocs folder.
main.php tries to pass a variable to test.php using an xmlhttprequest. test.php simply echos the variable. I then try and display the echoed result in main.php.
When I load main.php in a browser and view source, I expect to see the variable echoed. Instead I see an error message that the variable that was supposed to be passed to test.php is undefined.
Any help would be most welcome.
<html>
<head>
<title>Main</title>
<script type="text/javascript" src="../scripts/jquery-1.7.1.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","test.php?var1=hello", true);
xmlhttp.send(null);
<?php include("test.php"); ?>
});
</script>
</head>
<body></body>
</html>
Here is the code in test.php which resides in the same folder as main.php
<?php
$name=$_GET['var1'];
echo $name;
?>