Alan A
Alan A

Reputation: 2551

jquery passing an array to html form

I have a two dimensional array:

If I 'alert' the output as follows I see expected results:

alert(myArray[0][0]['test']);

I am then passing the array to an html form:

$('form#id1 #PassArray').val(myArray);

I am then lisening for the form submission and if I do this:

var received=$('input#PassArray').val();

alert(received[0][0]['test']);

I get output: undefined.

Is it necessary to prepare the array in some way if passing it to a html form?

Upvotes: 0

Views: 1484

Answers (1)

Kevin C.
Kevin C.

Reputation: 2527

IIRC, a form input can only have a string for its value.

You could either

  1. Serialize your array into a string with $.param()
  2. Iterate over your array to assign values to individual form fields. The form fields can have the same name with [] appended to the end to treat them as an array, i.e. <input name="array[]" />
  3. Just post the array with AJAX

Upvotes: 1

Related Questions