使用ajax设置会话变量 [英] Set session var with ajax

查看:121
本文介绍了使用ajax设置会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个session var.我有一个网页,其中有一些标签,当我彼此活跃时,这些标签不会重新充电.因此,我不知道如何设置会话变量.

I want to create a 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.

实际上,当用户将表单提交到此选项卡时,我的第一个选项卡将生成会话变量.我正在尝试使用Ajax.所以在我的ajax文件中,我有这个来设置我的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. So in my ajax file, I have this to set my 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);
 }

我可以得到第一个变量$tab_erreur.

I can get the first var $tab_erreur.

在我不知道如何为所有标签设置会话变量之后.我认为在ajax返回时,我将获得该值,并且可以对其进行设置和使用,但是我不确定.

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请求中发送一个数组:

I send an array in my ajax request like that :

$(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
    session_start();
    $_SESSION['data']=$_POST['data'];
    echo $_SESSION['data'];
    ?> 
});

数据是您通过POST发送的数据,现在echo可以将该数据或不同数量的数据作为响应返回到您的Ajax请求中.

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():

Using, $.post():

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

但是,$ .ajax()更好,因为您可以更好地控制流,如果成功,则可以进行此操作等.

However, $.ajax(), is much much better, since you have more control over the flow, and if success do this etc.

这篇关于使用ajax设置会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆