Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1433 How can I pass a JavaScript array to the next page when a form is submitted?

You are here: irt.org | FAQ | JavaScript | form | Q1433 [ previous next ]

When submitting a form, only the named form fields get passed onwards with their contents. The JavaScript code, and the objects you create are lost.

If you need to pass the data held within an array as part of the form, you need to place the data in a form field - a hidden form field is best.

Once the target page has loaded you need to parse the search data to extract the data to build another array.

On the first page, use something like:

<script language="JavaScript"><!--
function getArrayNumbers() {
  var myArray = new Array(1,2,3,4,5,6,7,8,9);
  document.input.data.value = myArray.join(',');
}
//--></script>

<form name="input" onSubmit="getArrayNumbers()">
<input type="hidden" name="data">
<input type="submit">
</form>

On the second page use something like:

<form name="output">
<textarea name="data" cols="2" rows="10"></textarea>
</form>

<script language="JavaScript"><!--
var namevalue = (location.search.substring(1)).split('=');
if (namevalue.length == 2) {
  var myArray = unescape(namevalue[1]).split(',');
  document.output.data.value = myArray.join('\r\n');
}
//--></script>

Feedback on 'Q1433 How can I pass a JavaScript array to the next page when a form is submitted?'

©2018 Martin Webb