0

I have a JS Array of Objects as follows:

var tables = new Array();

var table = new Object();

table.no=1001;
table.name="Table-1";
tables.push(table);

table.no=1001;
table.name="Table-1";
tables.push(table);

Now I need to send this tables object-array through a hidden field in a form. Can I directly assign the value of hidden field as tables object.

0

2 Answers 2

3

use JSON.stringify() to convert it to JSON string and send your data.

side note:

There is an issue in the way you are creating an appending objects. Each time you are editing the same object. It will result in all the elemnts in your array being the last object you made as they are all references to the same object you have. resolve this by creating new objects when you append items to the array

var tables = new Array();

tables.push({"no":1000,"name":"Table-0"});
tables.push({"no":1001,"name":"Table-1"});

alert(JSON.stringify(tables));

Sign up to request clarification or add additional context in comments.

Comments

0

You could stringify the value, put it in the field, send it to the server and then decode it. To stringify an object:

JSON.stringify(tables);

Assuming that you're using PHP on the server, you could decode it using json_decode function.

Comments

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.