0

I'm trying to use Jquery to pass an array to an HTML form as follows:

$('form#'+ID1+' input#myArray').val(theArray);

Then my form is as follows

<input type="hidden" id="myArray" name="myArray"> 

I then serialize before calling an Ajax request:

var dataString=$('form#grid1').serialize();

But on the receiving script I get:

Uninitialized string offset: 1

Any ideas?

Thanks!

8
  • Please show more code. This isn't helpful. Commented Mar 25, 2013 at 20:23
  • this makes no sense. please provide... i dont even know what i would want you to provide... more code Commented Mar 25, 2013 at 20:26
  • 'grid1' is the id of the form Commented Mar 25, 2013 at 20:26
  • then what is form# + ID1 + ? Commented Mar 25, 2013 at 20:28
  • Sorry it should be: $('form#'+grid1+' input#myArray').val(theArray); Commented Mar 25, 2013 at 20:34

1 Answer 1

1

Here's my best guess from what you are showing in the question.

When you call the following, the array represented by theArray is converted into a comma-separated string and placed in the hidden input element. This is the case even if theArray is a nested array.

$('form#'+ID1+' input#myArray').val(theArray);

But on the server, you are trying to treat the posted "myArray" value as an array, so you get the following error:

Uninitialized string offset: 1

The problem is that "myArray" is not an array, but is a comma-separated string.

I recommend formatting the theArray value as JSON before setting it into the hidden input element, especially if it is a nested array. You can do this:

$('form#'+ID1+' input#myArray').val(JSON.stringify(theArray));

Then parse it to an object on the server side. I'm guessing from the error message that you are using PHP, so you would do something like this:

$myArray = json_decode($_POST['myArray']);
Sign up to request clarification or add additional context in comments.

4 Comments

John S - you are the absolute Daddy. I bow before you, your solution worked perfectly. Thank you!!!
@user2056238 - I see you have asked 13 questions, but have never accepted an answer. If an answer solves your problem, you should check it as accepted. You can also up-vote an answer if it helps.
sorry, i'm quite new to this... where is the link to accept an answer?
I found it, just went back and clicked on 'answered' for previous questions. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.