I am working on a project that sends a ajax post request to a php file and then sets a php variable and returns the variable in js. That all works fine. What I am struggling with is being able call $name in the index.php file once getPhpVar() function has run.
This is what the program is supposed to do: 1.run getPhpVar() function 2.sends ajax post to get.php 3. get.php set name variable 4. get.php sends name variable back through js 5. I am able to call name variable in php Code: index.php
<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function getPhpVar(varname) {
$.post("get.php",
{
name: varname,
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
}
getPhpVar(11);
</script>
<?php echo $name;
?>
get.php:
<?php
if( $_REQUEST["name"] ) {
$name = $_REQUEST['name'];
echo $name;
}
?>