0

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'];
                }
            }
2
  • 1
    What is it that you get? Any error message? What is your problem? Commented Nov 17, 2010 at 17:25
  • Sorry, theres no error, but what i'm saying is that this is using ajax, what i want to do is like a hyperlink and actually go to the php page. Not just get an ajax response! Commented Nov 17, 2010 at 17:27

4 Answers 4

2

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'];
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

Why not use JSON

PHP JSON

Comments

0

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:

  1. actually POST from a form that the user fills out
  2. 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.

2 Comments

Thats correct, maybe i'm being naive but can i not just stick postData in a hyperlink or something? Is that possible?
@benhowdle89: you can put GET data in a hyperlink, not POST data. If your fields are only for pulling data, then GET is a reasonable way to go. But if you are changing things with the fields (e.g. updating a database), POST should be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.