【问题标题】:javascript array in php using Ajax on submit not workingphp中的javascript数组在提交时使用Ajax不起作用
【发布时间】: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


【解决方案1】:

你不需要自己字符串化,jquery会帮你,如果你把它字符串化,jQuery会相信你想要一个字符串来代替

var table_row = [];

table_row[0] = [123,123,123];
table_row[1] = [124,124,124];
table_row[2] = [125,125,125];



 $.ajax({
 type: "POST",
 url: "test1.php",
 dataType: "json",
 data: {myJSArray: table_row},
 success: function(data) {
                        alert("It is Successfull");
                        }
     });

但是,在 php 方面,您仍然需要对其进行解码,因为当您从 $_POST 获取它时,它始终是一个字符串。使用 json_decode 来完成。

$check1 = json_decode($_POST['myJSArray']);

【讨论】:

  • 我已经尝试过了,它仍然说注意:当我尝试获取会话值时未定义索引。有什么想法吗?
  • 你的会话也需要解码 json_decode($_SESSION['XX']);
  • 嗨,你说的是 test2.php 吗?
  • 您的页面和 php 脚本是否在同一个域(包括子域)上运行?如果不看stackoverflow.com/questions/2870371/…
  • Cors 似乎是问题所在,会话 cookie 未设置
【解决方案2】:

看看你的 test2.php

<?php 
session_start();
$test = $_SESSION['array'];
echo $test;
?>

如果只是文件中的代码,那么你得到的错误C:\xampp\htdocs\test\test2.php on line 13是无脑的,因为没有第13行,

但是,如果您对展示给我们的代码有所了解,之前可能会有所回应吗? 因为session必须在任何输出之前启动,

否则我已经测试了整个脚本并且工作正常......

要检查session 是否真的启动(否则$_SESSION 将不起作用),试试这个:

if(session_id())
{
    echo "Good, started";
}
else 
{
    echo "Magic! strangeness";
}

如果在test2.php 中没有发现问题,您可以在保存后检查test1.php echo $_SESSION['array'],并在您的javascript 回调函数警报data 参数本身中, 我相信你可以通过这种方式发现问题。

【讨论】:

  • 感谢您的回答。我已经检查了 test1.php 和 test2.php。如果我给出硬编码值两者都可以。这个问题并不局限于 javascript n ajax。该值不是来自 ajax 到 test1.php,它可以通过 $check1 = $_POST['myJSArray']; 收集
  • 然后你可以使用浏览器的开发者工具并检查network标签,在那里你可以看到任何ajax请求,响应等......
  • 我正在使用 mozilla firefox,我在哪里可以找到这个开发人员工具以及如何检查
  • OK 然后右键单击文档上的任意位置选择“检查元素”(Ctrl+Shift+k),您将看到Network 选项卡,保持打开状态,下一个重新加载页面(或者如果您执行 ajax 调用通过单击按钮等...,只需进行 ajax 调用),您将看到带有状态的网络活动(文件包括、ajax 调用),然后单击适当的项目以获取详细信息
【解决方案3】:

我搞定了,代码如下

Javascript 文件:在页面 index.php 中

你可以调用这个函数并传递参数或者直接使用代码

var table_row = []; //globally declared array
var table_row[0]=["123","123","123"];
var table_row[1]=["124","124","124"];
var table_row[2]=["125","125","125"];

    function ajaxCode(){
            var jsonArray = JSON.stringify(table_row)
             $.ajax
                        ({
                            url: "test1.php",
                            type: "POST",
                             dataType: 'json',
                            data: {source1 : jsonArray},
                            cache: false,
                            success: function (data) 
                            {
                             alert("it is successfull")

                             }

                        });
            } 

页面:test1.php

session_start();
unset($_SESSION['array']); 

$check1 = $_POST['source1'];    

$_SESSION['array']= $check1;
echo json_encode(check1);

Page: test2.php //我想从会话中获取值的最后一页

  if(session_id())
   {
    echo "Session started<br>";

    $test = $_SESSION['array'];
    echo "The session is".$test;

      }
   else 
  {
echo "Did not get session";
  }

 ?>

在索引页面中,我有一个提交的表单,提交时它调用 ajax 函数。 感谢您的帮助,非常感谢。

【讨论】:

    猜你喜欢
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多