for each jQuery loop to split into multiple strings for php parsing
I'm trying to take values from a table and put them into a string and send them to a php page for logging to a database, however there will be multiple rows need to be inserted so i need to split the values into multiple strings to send if that makes (any) sense?
This is my jQuery:
$('#submit').live('click',function(){
var supp_short_code=$('.supp开发者_开发问答_short_code').text();
var project_ref=$('.project_ref').text();
var om_part_no=$('.om_part_no').text();
var description=$('.description').text();
var cost_of_items=$('.cost_of_items').text();
var cost_total=$('.cost_total').text();
var dataString = 'string=//' + supp_short_code + '//' + project_ref + '//' + om_part_no + '//' + description + '//' + cost_of_items + '//' + cost_total
$.ajax
({
type: "POST",
url: "order.php",
data: dataString,
cache: false,
success: function()
{
alert("Order Submitted");
}
});
});
but what i'm saying that there may be 2 or 5 or 12 values in each of the variables!? My plan is to send a string to the php, get the php to parse the values between the "//" and to insert them into separate variables for a mysql query. If someone can think of a better way to do this, please tell me!!!
Transport it as JSON string.
[{"row": 1, "supp_short_code" : blah blah....}, {"row": 2, ....} ....]
Now this means you have to go through the rows one by one. Select on all the rows and construct your datastring one line at a time, by appending onto the JSON string. This will make the PHP parsing much easier.
var dataString = '['
$('#table tr').each(function(
var supp_short_code = $(this).filter('.supp_short_code').text();
// and so on.
if (index > 0) {
dataString += ','
}
dataString += '{"row": index, "supp_short_code": supp_short_code, .... }';
});
dataString += ']'
精彩评论