简体   繁体   English

将多维javascript数组传递给另一个页面

[英]pass multidimensional javascript array to another page

I have a multidimensional array that is something like this 我有一个多维数组,就像这样

  • [0]string [0]串
  • [1]-->[0]string,[1]string,[2]string [1] - > [0]串,[1]串,[2]串
  • [2]string [2]串
  • [3]string [3]串
  • [4]-->[0]string,[1]string,[2]string[3]string,[4]string,[5] INFO [4] - > [0] string,[1] string,[2] string [3] string,[4] string,[5] INFO

(I hope that makes sense) (我希望这是有道理的)

where [1] and [4] are themselves arrays which I could access INFO like myArray[4][5]. 其中[1]和[4]本身就是我可以像myArray [4] [5]那样访问INFO的数组。

The length of the nested arrays ([1] and [4]) can varry. 嵌套数组的长度([1]和[4])可以是varry。

I use this method to store, calculate, and distribute data across a pretty complicated form. 我使用这种方法在一个非常复杂的表单中存储,计算和分发数据。

Not all the data thats storred in the array makes it to an input field so its not all sent to the next page when the form's post method is called. 并非所有在数组中存储的数据都会使其成为输入字段,因此在调用表单的post方法时,并非所有数据都会发送到下一页。

I would like to access the array the same way on the next page as I do on the first. 我想在下一页上以与第一页相同的方式访问数组。


Thoughts: 思考:

Method 1: 方法1:

I figure I could load all the data into hidden fields, post everything, then get those values on the second page and load themm all back into an array but that would require over a hundred hidden fields. 我想我可以将所有数据加载到隐藏字段中,发布所有内容,然后在第二页上获取这些值并将它们全部加载回数组,但这需要超过一百个隐藏字段。

Method 2: 方法2:

I suppose I could also use .join() to concatenate the whole array into one string, load that into one input, post it , and use .split(",") to break it back up. 我想我也可以使用.join()将整个数组连接成一个字符串,将其加载到一个输入中,发布它,并使用.split(“,”)来重新打破它。 But If I do that im not sure how to handel the multidimensional asspect of it so that I still would be able to access INFO like myArray[4][5] on page 2. 但是,如果我这样做,我不知道如何处理它的多维度,以便我仍然可以像第2页上的myArray [4] [5]那样访问INFO。

I will be accessing the arrary with Javascript, the values that DO make it to inputs on page 1 will be accessed using php on page 2. 我将使用Javascript访问arrary,将在第2页上使用php访问第1页上的输入值。


My question is is there a better way to acomplish what I need or how can I set up the Method 2 metioned above? 我的问题是有更好的方法来完成我需要的东西, 或者我如何设置上面提到的方法2?



This solved my problem: 这解决了我的问题:

var str = JSON.stringify(fullInfoArray);
sessionStorage.fullInfoArray = str;

var newArr = JSON.parse(sessionStorage.fullInfoArray);

alert(newArr[0][2][1]);

If possible, you can use sessionStorage to store the string representation of your objects using JSON.stringify() : 如果可能,您可以使用sessionStorage使用JSON.stringify()存储对象的字符串表示形式:

// store value
sessionStorage.setItem('myvalue', JSON.stringify(myObject));

// retrieve value
var myObject = JSON.parse(sessionStorage.getItem('myvalue'));

Note that sessionStorage has an upper limit to how much can be stored; 请注意, sessionStorage有一个可以存储多少的上限; I believe it's about 2.5MB, so you shouldn't hit it easily. 我相信它大概是2.5MB,所以你不应该轻易打它。

Keep the data in your PHP Session and whenever you submit forms update the data in session. 将数据保存在PHP会话中,每当提交表单时,都会更新会话中的数据。 Every page you generate can be generated using this data. 您生成的每个页面都可以使用此数据生成。

OR 要么

If uou are using a modern browser, make use of HTML5 localStorage. 如果您使用的是现代浏览器,请使用HTML5 localStorage。

OR 要么

You can do continue with what you are doing :) 你可以继续你正在做的事情:)

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

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