I have a form where fields get added via JavaScript. This is fairly easy to store without a bunch of extra JS code (for deleting and reordering) by using arrayed names for fields.
<input type="text" name="product[]" />
The problem comes when I need a multi-dimensional arrayed list of form data.
order
request_date
product[]
quantity[]
warehouse1
warehouse2
etc...
Basically, each order has the order data, multiple products, and each product line item can be pulled from one or more warehouses. If the line item’s order quantity is 100, and the first choice warehouse only has 50 units, they will need to select a secondary warehouse to get the second 50 units from…
I am trying to get this all onto one page possibly with JS popups to select between the different warehouses.
The problem is when I try to think of a good way to send this info to the server.
I would like to use the arrayed line item info, but cannot figure out how to send the multiple warehouses per line item back to the server. Should I just do something like comma separated values in a hidden field (populated by the JS warehouse selection popup)? Is there a better way? Any suggestions appreciated. Thanks.
If it matters, the backend is PHP.
Why not use JSON? On the PHP end you’ll end up with an array of elements for the nested arrays.
It sounds to me like you’re either unaware of JSON (and with an SO rep of 3k, I can’t believe that) or that you’re unaware that you can pass JSON back to the server.
But for what you’re describing, that sounds like a natural and perfect fit.
“stick that array element as an object onto the returned value”
.eachwhich is ugly and nasty. There are better ways to do this, if you want speed or cleanness. But this works..dataassociated with it, of key'myKey'. You could instead iter over each cell in a table, or whatever. This is how I do it, and probably leads to bloated pages anyways. But when I append a row I attach the value as an object so I can do this sort of thing faster (faster to type that is)