【发布时间】:2014-09-06 12:43:52
【问题描述】:
我有一个从 javascript 传递到 php 的数组,在 php 页面中,我试图将它放在会话中以在第三页中使用。代码如下
JavaScript:
var table_row = [];
table_row[0] = [123,123,123];
table_row[1] = [124,124,124];
table_row[2] = [125,125,125];
var jsonString = JSON.stringify(table_row);
$.ajax({
type: "POST",
url: "test1.php",
dataType: "json",
data: {myJSArray: jsonString},
success: function(data) {
alert("It is Successfull");
}
});
test1.php
<?php
session_start();
$check1 = $_POST['myJSArray'];
$_SESSION['array']= $check1;
echo $check1;
?>
test2.php
<?php
session_start();
$test = $_SESSION['array'];
echo $test;
?>
在提交时,我在 javascript 中调用该函数,表单将我带到 test2.php。它在 test2.php 页面上给出错误通知:未定义索引:第 13 行 C:\xampp\htdocs\test\test2.php 中的数组
任何建议都请告诉我。
【问题讨论】:
-
尝试将该索引重命名为其他名称。在 test1.php 中,确保您获取的是通过 ajax 发送的 JSON 值。
-
test1 是否正确回显?为了测试,为什么不让你的 ajax 脚本临时回显返回的值呢?它应该与您发送的 JSON 字符串相同。
标签: javascript php jquery ajax arrays