jquery :: how to store and send this data
basically I have a for loop that give me two values,
id, and value on each loop
now,
I have no idea what either of the values will be.
Anyway
there can be an unlimited number of these.
But I need to store them in a way that I can send them using jquery's ajax.
in a way that on the other end开发者_如何学C (in php),
I can loop over them in a simple foreach
loop
I was thinking json but have no idea how to create the json object in the javascript for loop.
Simply store them in an object
var data = {};
for (/* your for loop */) {
// get "id" and "value" vars
data[id] = value;
}
$.post('processor.php', data);
Then in your PHP file, loop over the $_POST
array
if (isset($_POST)) {
foreach ($_POST as $id => $value) {
// tada
}
}
Edit: If you wanted to isolate the id/value data from other post data, you could build a named post data array like so
var data = {};
for (/* your for loop */) {
// get "id" and "value" vars
var key = "dataValues[" + id + "]";
data[key] = value;
}
$.post('processor.php', data);
And process it like this
if (isset($_POST['dataValues']) && is_array($_POST['dataValues']) {
foreach ($_POST['dataValues'] as $id => $value) {
// tada
}
}
for ( var x in someArray ) {
data = {id : x, value : someArray[x]};
$.get(URL, data);
}
精彩评论