显示会话var而不刷新页面

Display a session var without refresh the page

本文关键字:刷新 会话 var 显示      更新时间:2023-09-26

我在ajax文件中设置了一个会话var,如下所示:$_SESSION['id_essai']=$ligne[1];。此var的值应在每次提交表单时更改。

在不充值页面的情况下,我想得到这个变量的值。所以我这样回应它:echo $_SESSION['id_essai'];我的问题是,当我尝试回显它时,我会得到以前的值,直到我给页面充电。我想刷新这个var而不给页面充电。我该怎么做?

编辑:

我向我的ajax文件发送了一个可手动操作的网格,如下所示:

$(document).ready(function(){
        $('#submit_button_essai').click(function(){
            $.post("ajax_insert_essai.php",{arr:data_essai}, insert_essai_callback,'json'); 
            return session;
        });
    });

在这个ajax文件中,我用这个网格的一个值声明我的会话var,我可以这样返回它:

array_unshift($tab_erreur,$_SESSION['id_essai']);
    echo json_encode($tab_erreur);    

因此,我发送了一组错误和会话变量,我在回调函数中处理这些错误和变量

function insert_essai_callback(responseObject,ioArgs) //ajax renvoie les indices des cases erronnées et les messages correspondants.
{
var jsonobject = eval(responseObject);
console.log(jsonobject);
//alert(jsonobject[0]);
var session = jsonobject[0];
for(var item in jsonobject)
{       
        if((item % 2 ) == 0) //Si l'indice est pair, on affiche la couleur
        {
            $("td").eq(jsonobject[item-1]).after("<div class='"message'">"+jsonobject[item]+"</div>");
        }else //Si l'indice est impair, on affiche le message 
        {
            $("td").eq(jsonobject[item]).css("background-color","red");
        }   
}
}

我的会话var在jsonobject[0]中。这是我在ajax中尝试返回会话var之后的代码。

我认为问题是每个reqest都会在服务器上启动一个新的会话。

<?php
session_start();
// Restart the last session if a session-id is given
if (isset($_GET['session-id'])
    session_id($_GET['session-id']);
/* ... your code here .. */
$response = array('session-id' => session_id());
/* ... add some more values/results to your response */
header('Content-Type: application/json');
die(json_encode($response));
?>

设置一个新的PHP文件。然后在单击事件或更改事件或其他事件上使用ajax将需要更新的值发送到会话变量。当接收到该值时,PHP将更新会话值。然后以JSON形式返回该变量的值。Tada你得到了更新的数值。我希望你能理解。。

示例代码看起来像这个

在ajax中(我在这里使用的是jquery ajax(

 $.ajax({
  url : 'session.php',
  method : 'POST',
  data : your value to update ,
  dataType : json,
  success : function(data){
      alert(data.value);
   }

 })

在session.php文件中

  <?php
    session_start();
    $value = $_POST['value'];
    $_SESSION['value'] = $value;
    $json = array('value' => $_SESSION['value'] );
    print_r(json_encode($json));

  ?>

如果您需要在不刷新的情况下获取值,请在事件上调用ajax函数。然后将返回的数据更新到div或其他文件中。我希望这个有帮助!:(

创建一个process.php文件,post/get就是form

<form method = 'post' action = 'process.php'>
    <input type = 'hidden' value = 'some_Hidden_Value_that_You_Want' id = 'hidden_id'>
    <!--Some other stuff (Grid may be) -->
    <input type = 'button' value = 'Submit' id = 'submit_form'>
</form>
$("#hidden_id").click(function(){
    var hidden_id = $('#hidden_id').val()
    $.ajax({
        url: 'process.php',
        type: 'POST',
        data:{
            want_this: hidden_id
        },
        success: function(posted){
            alert(posted);
        }
    });
});

这将把你的东西发布到处理页面。

和在process.php或在您的情况下在ajax_insert_essai.php

这样做可以查看你得到的

<?php print_r($_POST); ?>

编辑

callback

obj = $.parseJSON(jsonobject[0]);
console.log(obj);
// this was to update the page without refreshing the page
//          $this = $('#'+obj.ID).find('td');
//          $this.eq(2).html(obj.username);
//          $this.eq(3).html(obj.password);
//ignore these lines
/*You can access all your stuff then like that*/
condole.log('some id' + obj.id);
//and so on...

如果将成为新会话变量的数据是从页面发送的,那么让它也使用页面上已有的数据,使用JavaScript更新到页面。

如果要返回的数据是从页面发送的,则无需重新获取数据,因为它已经在页面上了。