问题
my problem is as follows (Question updated):
I've got two pages:
1.php and 2.php
I'm trying to get a PHP variable (in this very case, a SESSION variable) from 2.php to 1.php through an AJAX request.
This is the script in 1.php
jQuery('#refresh').click(function(e){
e.preventDefault();
jQuery.ajax({
type:'POST',
url: '2.php',
data: { sessionVar: '<?php $PHPvariable ?>' },
success:function(response){
alert(sessionVar);
}
})
});
For your better understanding, this script is called from within the 1.php file, so to properly inject PHP into the js. As you can see I'm trying to retreive the $PHPvariable variable declared in 2.php.
Am I doing it correctly??
回答1:
Example with JQuery
In 1.php
$.ajax({
url: "2.php"
})
.done(function( data ) {
// data should return your variable
});
See here for more : jQuery.ajax()
回答2:
what you can do is make jquery call php file which will get your session value:
function GetSessionValue(sesname){
$.post("getsession.php", {sesname: ""+sesname+""}, function(data){
if(data.length >0) {
//do what you want with your session value
}
});
}
and in your php file write only
$sesname = $_POST['sesname'];
echo $_SESSION[$sesname];
来源:https://stackoverflow.com/questions/25010470/get-current-session-variable-without-page-refresh