简体   繁体   English

使用ajax设置会话变量

[英]Set session var with ajax

I want to create a session var . 我想创建一个session var I have a web page with some tabs that don't recharge when I active one another. 我有一个网页,其中有些标签页在我彼此活跃时不会重新充电。 So, I don't know how to set my session var. 因此,我不知道如何设置会话变量。

Indeed, my first tab will generate the session var when the user submit the form into this tab. 实际上,当用户将表单提交到该选项卡时,我的第一个选项卡将生成会话变量。 I'm trying to do it with ajax. 我正在尝试使用Ajax。 So in my ajax file, I have this to set my var : 所以在我的ajax文件中,我有这个来设置我的var:

if(pg_num_rows($res) == 1)
 {                                  
    $flag=false;
    $message = "L'identifiant de l'essai existe déjà dans la base";
    array_push($tab_erreur,$cpt,$message);
  }else {
    $sessionIDEssai=$ligne[1]; //Here is my session var
  }  

After, I want to return that value with an other like this : 之后,我想用另一个返回该值:

echo json_encode($tab_erreur),$sessionIDEssai;

First of all I don't know if it's correct, because I can't get it in my callback function. 首先,我不知道它是否正确,因为我无法在回调函数中获取它。

 function insert_essai_callback(responseObject,ioArgs) .
 {
    var jsonobject = eval(responseObject);
    console.log(jsonobject);
 }

I can get the first var $tab_erreur . 我可以得到第一个var $tab_erreur

And after I don't know how to set my session var for all my tabs. 在我不知道如何为所有标签设置会话变量之后。 I think that at the return of the ajax, I will get the value and I could set it and use it, but I'm not sure. 我认为在ajax返回时,我将获得该值并且可以设置并使用它,但是我不确定。

EDIT 编辑

I send an array in my ajax request like that : 我像这样在ajax请求中发送一个数组:

$(document).ready(function(){
        $('#submit_button_essai').click(function(){

            $.post("ajax_insert_essai.php",{arr:data_essai}, insert_essai_callback,'json'); 
        });
    });

Ajax 阿贾克斯

 $.ajax({
        type : 'POST',
        url : './session.php',
        dataType: "json",
        data: data,
        success : function(data){},
        error : function(){}
 });

PHP PHP

<?php
    session_start();
    $_SESSION['data']=$_POST['data'];
    echo $_SESSION['data'];
    ?> 
});

Data is what you send through a POST, now echo can return that data or a different amount of data to your Ajax request as a response. 数据就是您通过POST发送的数据,现在echo可以将该数据或不同数量的数据作为响应返回到您的Ajax请求。

Using, $.post(): 使用$ .post():

$.post({
    url: url,
    data: data,
    success: success,
    dataType: dataType
});

However, $.ajax(), is much much better, since you have more control over the flow, and if success do this etc. 但是,$ .ajax()会好得多,因为您可以更好地控制流,如果成功,则可以这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM