2013-08-06 32 views
0

你好朋友一个新手问题。我对编程很陌生,因此请温和。JavaScript/PHP/Ajax设置多个会话变量

我想使用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']; 
?> 

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

这个想法是将由各种JavaScript函数派生出来的各种值发布到会话以供PHP使用。

请帮忙。

UPDATE:

它是这个样子,作出该变量的值可以用只有JavaScript的帮助来计算。

+1

男子我建议你将你的php变量保存到隐藏的div中。然后,您可以轻松使用和更新这些值,并且更加干净! –

+0

我不认为使用隐藏的div会给你的应用程序带来问题。我没有看到一个理由。他们不占用空间,像他们不存在,他们只是举起了价值。 –

回答

1

您必须将参数与连字符连接起来(&)。

使用此行

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

BTW:我真的建议使用jQuery或AJAX请求类似的库。此外,我会使用JSON交换数据或JavaScript数组,其中键名是变量的名称。

+0

感谢您的帮助。那么这行在'index.php'中怎么办?'if((empty($ _ SESSION ['function1Val']))&(empty($ _ SESSION ['function2Val']))&&(empty($ _ SESSION ['jsStatus'])))'还有代码的其他部分? –

+0

我真的不明白这个问题。 你在问什么是更清洁的解决方案吗? 一个简单的黑客应该是在PHP中创建一个数组,其中的键是变量名,值是这些的JS代码。 – Pat

+0

但是,对XSS注入使用良好的保护措施(例如,避免使用预定义的函数名称而不是纯Javascript代码)。如果真的需要,我可以提供一个例子明天。 – Pat