php 无需页面刷新即可更新会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17963243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:43:58  来源:igfitidea点击:

update session variable without page refresh

phpjavascriptjqueryajax

提问by Karvin Mendis

I need to update the session variable on page 1, when click on a link in iframe (external page). session variable is successfully set when i click on iframe's link. the problem is i need to refresh the page 1 to update that session variable on page1.

单击 iframe(外部页面)中的链接时,我需要更新第 1 页上的会话变量。当我单击 iframe 的链接时,会话变量已成功设置。问题是我需要刷新第 1 页以更新第 1 页上的会话变量。

enter image description here

在此处输入图片说明

how to resolve this.any ajax , jquery help. THanks

如何解决 this.any ajax ,jquery 帮助。谢谢

回答by Bora

HTML a hreflink:

HTMLa href链接:

<a href="#" id="clickme">Update Session</a>

jQuery codes:

jQuery 代码:

You can send data with line of data: { varname: 'here we go'}

您可以使用以下行发送数据 data: { varname: 'here we go'}

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$("#clickme").click(function(e) {
    e.preventDefault();
    $.ajax({
        type:'POST',
        url:'update.php',
        data: { varname: 'here we go'},
        success:function(response){
           alert(response);
        }
    });
});
</script>

update.php file:

update.php 文件:

You can get sent var with $_POST["varname"]

你可以得到发送 var $_POST["varname"]

<?php
   $_SESSION["varname"] = $_POST["varname"];
   echo $_SESSION["varname"];
?>