Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | form | Q1433 [ previous next ]

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

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.