繁体   English   中英

如何在PHP的会话中创建AJAX响应?

[英]How to create AJAX response into sessions in PHP?

我想在PHP的会话中创建AJAX响应。 这是我的代码,当我单击此按钮时:

<input type="button" id="callfunction" value="submit">

我称这个jQuery:

     $(document).ready(function(){
        $("#callfunction").click(function(){
        $.ajax({
        url:"ajax.php",
        type:"POST",
        data:"first_variable="+first_variable+"&second_variable="+second_variable,
        success:function(resp)
        {
            // can I create my resp into session here....?
        }

      });
    });
});

在ajax.php页面中,我有一些此值$first_variable$second_variable

ajax.php
$first_variable = $_POST['first_variable'];
$second_variable = $_POST['second_variable'];
echo $first_variable;
echo $second_variable;

现在在警报响应中,我vl收到first_variable和second_variable的警报消息

现在我的问题是要将这两个变量转换为会话,以便可以在该页面中使用它而不将其作为隐藏变量传递,是否有任何方法可以在响应函数中将其转换为会话,否则我将在某些地方出错?

ajax.php中,您必须初始化session_start(); ;。 如果不初始化,则会话将无法进行。 在php.net上读取session_start()

session_start();

$first_variable = $_POST['first_variable'];
$second_variable = $_POST['second_variable'];


$_SESSION['first_variable'] = $first_variable;
$_SESSION['second_variable'] = $second_variable; 

在ajax.php本身中创建会话

ajax.php

    $first_variable = $_POST['first_variable'];
    $second_variable = $_POST['second_variable'];
    echo $first_variable;
    echo $second_variable;    
    $_SESSION["first_var"] = $first_variable;
    $_SESSION["sec_var"] = $second_variable;

在获得响应后创建会话毫无意义,因此请在生成响应之前创建会话,

在ajax.php中

<?php

start_session();  // Since we are using sessions;

$first_variable = $_POST['first_variable'];
$second_variable = $_POST['second_variable'];

$_SESSION[first_variable] = $first_variable;
$_SESSION[second_variable] = $second_variable;


echo $first_variable;
echo $second_variable;

?>

暂无
暂无

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

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