how to create a multi dimensional array out of dynamically created fields and variables
here is my url: http://iadprint.com/products?product=business%20card
the fields that you see on this page are all dynamically created on the backend. i decided to use the fieldnames created as variables to display the price as you can see in the js below. I have two problems one being that when the page refreshes the pricing div values cleans out and the selections do not stay and the second one is i cant figure out how to add all items selected into an array for the ca开发者_如何学Pythonrt page.
for the first problem i figured it was best to place each selection into a cookie in an array form and on refresh pull out the data and select the needed fields. unless there is another way.
this is what im guessing the array would look like
$products = array("product" => "business_card", array("ProdID" => "1"), ("ProdID" => "2") ..and so on)
basically i would add only the fields who have a length of greater than zero so that i know it was selected. what my question is would that be the correct way to format the array? and is there a way i can do array_push in javascript to push in new elements each time a selection change is occurred?
as for problem two im hopefully guessing it will be easy once the cookie is created with its correct data.
The best way, imo to handle selection in javascript is by using an object on the form
{<id1>: true, <id2>:true, etc.}
The reason for this is that it is incredibly fast to check if something is selected, and it is very easy to convert it to an array if needed (just extract the keys).
Keeping track of a selection using an array means you have to loop over that array everytime you need to check if something is selected.
for the first problem i figured it was best to place each selection into a cookie in an array form and on refresh pull out the data and select the needed fields. unless there is another way.
You could use AJAX and PHP session vars. On selection change, you send the selection via AJAX to a PHP page which sets some session data. When the page loads, you use the session data to set the selected fields.
is there a way i can do array_push in javascript
var arr = [];
arr.push(obj1);
More on push and other js array methods: http://www.javascriptkit.com/jsref/arrays.shtml
精彩评论