• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript array object to parameter

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a javascript array, I want to append to request parameter, how could I do this ?

like,
var myArray = new Array();
var people = new Object();
people.name="Tom";
people.age="30";
myArray[0]=people;

var people1 = new Object();
people1.name="Bob";
people1.age="50";
myArray[1]=people1;

Then my paramter will be
?people[0].name=tom&people[0].age=50&people[1].name=bob&people[1].age=50.

So far, my way is , use hidden value, like
document.write("<input name='people[0].name' type='hidden' value='tom'>");
....

But I think this way not efficient and use lots of memory. Do you have better way?

Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look for buildQueryString here: http://www.oracle.com/technology/pub/articles/cioroianu-ajax-data.html
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But I can't use AJAX.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward, did you have a look at the function called buildQueryString ?
Don't you think that it is a simple Javascript function ?
Copy/Paste that function and get rolling
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Satou,
My problem is not located in how to make a right-format query but in how to append an array object to query when submiting.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Edward, that function would not do the work then.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic