pass an array from jQuery to PHP (and actually go to the page after submit)
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 jQ开发者_C百科uery:
$('#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'];
}
}
You can store the received data in a session:
if (isset($_POST['data']) && is_array($_POST['data'])) {
session_start();
$_SESSION['data'] = $_POST['data'];
}
And then link to another PHP page that retrieves the data from the session and displays it:
session_start();
if (isset($_SESSION['data']) && is_array($_SESSION['data'])) {
foreach ($_SESSION['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.
$('#preview').live('click',function(){
var cForm = $('<form method="post">').attr('action', "preview.php");
$('#items tr').not(':first').each(function(index, value) {
var keyPrefix = 'data[' + index + ']';
cForm.append($('<input type="hidden">').attr('name', keyPrefix + '[index]').val(index));
cForm.append($('<input type="hidden">').attr('name', keyPrefix + '[supp_short_code]').val($(this).closest('tr').find('.supp_short_code').text());
// etc
});
cForm.hide().append('body').submit();
return false;
});
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.
Why not use JSON
PHP JSON
So you are current creating a POST string using jQuery and then sending it using ajax to a php script. Now you actually want to POST directly to PHP. Is that correct?
If I've got that right, there are 2 options:
- actually POST from a form that the user fills out
- have a skeleton form consisting of
the
<form>
tags and a submit button. Use jQuery to create and populate hidden form fields when the user hits the submit button and before the submit action is completed.
精彩评论