JQuery Post 发送数组,它 returns PHP 中的成功数组但是当页面加载时没有任何显示?

JQuery Post sends arrays and it returns the successful arrays in PHP but when the page is loaded nothing shows up?

所以我使用 JQuery 到 post 三个数组到 PHP 脚本,PHP 脚本获取这些 $_POST,然后将它们设置为每个 $_SESSION 变量。然后我回显或 print_r 它到脚本页面和 JQuery returns 在控制台中输出 console.log。好像顺利通过了。但是,当我加载页面时,它会显示数组名称,但它们是空的。为什么?

JQuery代码:

var postnames = stripnames;
var postqty = stripqty;
var postprices = stripprices;

if (stripqty[0] != 0 && stripqty[0] != null) {
    $.post("php/submit.php", { 'postnames':postnames, 'postqty':postqty, 'postprices':postprices }, function(returned) { 
        console.log(returned);
        routput = returned;
    });

    //Now we redirect our customer to the information filling in page.
    //window.location.assign(window.location.hostname + "php/submit.php");

} else {
    document.getElementById('outputresult').innerHTML = "Please enter a quantity for at least one strip box.";  
}

PHP submit.php

代码
<?php
session_start();

include 'session.php';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sell to Us</title>
</head>

<body>

<?php
print_r($_SESSION['snames']);
?>

</body>
</html>
session.php
<?php
session_start();

include 'session.php';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sell to Us</title>
</head>

<body>

<?php
print_r($_SESSION['snames']);
?>

</body>
</html>

session.php:

<?php

    $_SESSION['snames'] = $_POST['postnames'];
    $_SESSION['sqty'] = $_POST['postqty'];
    $_SESSION['sprices'] = $_POST['postprices'];


?>

这是 JQuery 输出:

"6b99aca9426f41fbbeaf4e291115ba7d

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Sell to Us</title>

</head>



<body>



Array
(
    [snames] => Array
        (
            [0] => Ultra Blue 25
        )

    [sqty] => Array
        (
            [0] => 2
        )

    [sprices] => Array
        (
            [0] => 40
        )

)


</body>

</html>"

然后这是我加载浏览器时在页面上显示的内容:

6b99aca9426f41fbbeaf4e291115ba7d Array ( [snames] => [sqty] => [sprices] => ) 

开头的数字是会话ID。我可以 post 显示在 JQuery 所在主页上的会话 ID,但我怀疑它是否真的重要,因为它与那里处理的 PHP 会话变量无关?或者是吗?我确实检查过并且数字匹配。可能是我看的不够仔细

如果你们想看实时工作版本,那么我很乐意 link 在这里和 post DropBox URL 每个文件最新工作版本。

编辑:我关闭了浏览器,打算晚上关掉电脑,但显然 JQuery 返回与正常情况相同的输出,但是当我直接加载 PHP 脚本时在浏览器中像以前一样没有显示。

编辑 2/2/15

<?php
session_start();

//We have to use isset because if we don't then ever subsequent pageload will overwrite the information in SESSION with nothing since we are not posting anything those times. So we check to see if POST isset so we know if there is no new information then to keep the old information.

//if(isset($_POST['postnames'])) unset($_SESSION);


if(isset($_POST['acceptpost'])) {

    unset($_SESSION);
    echo('Posted');

    $_SESSION['snames'] = $_POST['postnames'];
    $_SESSION['sqty'] = $_POST['postqty'];
    $_SESSION['sprices'] = $_POST['postprices'];

    //if(isset($_POST['postnames'])) $_SESSION['snames'] = $_POST['postnames'];
    //if(isset($_POST['postqty'])) $_SESSION['sqty'] = $_POST['postqty'];
    //if(isset($_POST['postprices'])) $_SESSION['sprices'] = $_POST['postprices'];

}

echo("There are " . count($_POST['postnames']) . " variables to be written.");



?>

这是我当前的 session.php 代码。

当您浏览到 php 脚本时,$_POST 数组为空,因为您没有通过该请求通过 post 发送任何数据。因此,您在 session.php:

中覆盖了会话变量
$_SESSION['snames'] = $_POST['postnames'];
$_SESSION['sqty'] = $_POST['postqty'];
$_SESSION['sprices'] = $_POST['postprices'];

因此print_r($_SESSION['snames']);什么都不输出。

要解决您的问题,您应该只在 jquery ajax 调用真正发送 post 数据时写入会话变量。有几种方法可以实现,例如:

if(isset($_POST['postnames'])) $_SESSION['snames'] = $_POST['postnames'];
if(isset($_POST['postqty'])) $_SESSION['sqty'] = $_POST['postqty'];
if(isset($_POST['postprices'])) $_SESSION['sprices'] = $_POST['postprices'];