Get current session variable without page refresh

核能气质少年 提交于 2019-12-25 18:57:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!