This seems like it would have been simple, but what i was doing was creating an array in jQuery and sending it to a php via ajax and inserting the records into a db. But what i want to do now is create the array exactly the same but instead of ajax, i’d like to go to the php page and view what it has received.
How would i go about doing this?
I’m using this jQuery:
$('#preview').live('click',function(){
var postData = {};
$('#items tr').not(':first').each(function(index, value) {
var keyPrefix = 'data[' + index + ']';
postData[keyPrefix + '[index]'] = index;
postData[keyPrefix + '[supp_short_code]'] = $(this).closest('tr').find('.supp_short_code').text();
postData[keyPrefix + '[project_ref]'] = $(this).closest('tr').find('.project_ref').text();
postData[keyPrefix + '[om_part_no]'] = $(this).closest('tr').find('.om_part_no').text();
postData[keyPrefix + '[description]'] = $(this).closest('tr').find('.description').text();
postData[keyPrefix + '[quantity_input]'] = $(this).closest('tr').find('.quantity_input').val();
postData[keyPrefix + '[cost_of_items]'] = $(this).closest('tr').find('.cost_of_items').text();
postData[keyPrefix + '[cost_total_td]'] = $(this).closest('tr').find('.cost_total_td').text();
});
$.ajax
({
type: "POST",
url: "preview.php",
dataType: "json",
data: postData,
cache: false,
success: function()
{
}
});
});
And this PHP:
if (isset($_POST['data']) && is_array($_POST['data'])) {
foreach ($_POST['data'] as $row => $data) {
echo $data['index'];
echo $data['project_ref'];
echo $data['supp_short_code'];
echo $data['om_part_no'];
echo $data['description'];
echo $data['quantity_input'];
echo $data['cost_of_items'];
echo $data['cost_total_td'];
}
}
What you can do, following on from what dnagirl said, is to generate the POST form using jQuery and submit it.
The reason for using attr() in places rather than inline within the creation string is to avoid issues with escaping, and it looks cleaner in my opinion.