繁体   English   中英

JavaScript / PHP / Ajax设置多个会话变量

[英]JavaScript / PHP / Ajax set multiple session variables

朋友您好,一个新手问题。 我是编程新手,因此请保持谦虚。

我试图使用JavaScript发布多个会话变量,以便以后可以在我的PHP中的多个位置使用它们。

我的index.php文件

<?php
   session_start();
?>
<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <?php 
         if ((empty($_SESSION['function1Val'])) & (empty($_SESSION['function2Val'])) && (empty($_SESSION['jsStatus']))) {
            echo '<script type="text/javascript" src="vals.js"></script>
            <script type="text/javascript">
            var session=false;
            var jsStatus;
            var function1Val;
            var function2Val;
            </script>';
         } else {
            echo '<script type="text/javascript"> 
            var session=true; var jsStatus=' . $_SESSION['jsStatus'] . ';
            var session=true; var function1Val=' . $_SESSION['function1Val'] . ';
            var session=true; var function2Val=' . $_SESSION['function2Val'] . ';
            </script>';
         }
      ?>
   </head>
   <body>
    <?php

echo $jsStatus;
echo $function1Val;
echo $function2Val;

session_destroy ();
         ?>
   </body>
</html>

我的vals.js文件

window.onload = function() {
    // if there is no session (session = false)
    if (!session) {

        // Set value to variable jsStatus
        jsStatus = 'enabled';

        // Call function to get function1
        function1();

        // Call function to get function2
        function2();

        // Make ajax call to php page to set the session variable
        setSession();
    }
}

function function1() {

    // code to get value goes here
    function1Val = 'result1';
}

function function2() {

    // code to get value goes here
    function2Val = 'result2';
}

function setSession() {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        // Reload the page
        window.location.reload();
        }
      }
    xmlhttp.open("POST","session.php?function1Val=" + function1Val,true);
    xmlhttp.send();

    // like this I want to transfer other values of 'function2Val' and 'jsStatus'
}

我的session.php文件

<?php
    session_start();

    // check if it is already set if you like otherwise:
    $_SESSION['function1Val'] = $_REQUEST['function1Val'];
    $_SESSION['function2Val'] = $_REQUEST['function2Val'];
    $_SESSION['jsStatus'] = $_REQUEST['jsStatus'];
?>

我知道代码很乱,但是我不知道如何编写正确的语法。 我实际上试图修改单个变量代码,但失败了。 因此需要帮助。

想法是将各种JavaScript函数派生的各种值发布到会话中,以供PHP使用。

请帮忙。

更新:

之所以必须这样,是因为只能通过JavaScript来计算所述变量的值。

您必须将参数与“&”号连接起来。

使用此行

xmlhttp.open("POST","session.php?function1Val=" + function1Val+"&function2Val=" + function2Val+"&jsStatus=" + jsStatus,true);

顺便说一句:我真的建议对AJAX请求使用jQuery或类似的库。 此外,我将使用JSON交换数据或Javascript数组,其中键名称是变量的名称。

暂无
暂无

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

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