1

I know that this question has been asked in many different ways before. I am having difficulty parsing a JSON encoded array to another page within my domain.

I have this php code:

<?php 
    $json_results = json_encode($results);
?>

Here is an example of $json_results:

[{"Status":"example status 2","Contact ID Number":"1","Date Entered":"2011-06-15","Date Assigned":"2011-06-15","Date Last Modified":"2011-06-15","Deceased Date":null,"Linked To Registrant 1":"Mike Murdoc","Relationship 1":"Father","Authorization 1":"1","Linked To Registrant 2":"Sam Murdoc","Relationship 2":"Husband","Authorization 2":"1","Location":"Zues Funeral Home","Sales Manager":"Dan Thompson","Counselor":"Steve Brown","Registration Number":"1","Registration Date":"2001-05-11","Program 1 Number":"2","Program 1 Date":"2003-11-05","Program 1 Name":"Non Registrant Test Program - Advantage","Program 2 Number":"--","Program 2 Date":"--","Program 2 Name":"--","First Name":"Onelia","Last Name":"Murdoc","Address Location 1":"home","Address 1":null,"City 1":"Houston","State/Province 1":"Texas","Zip/Postal Code 1":"77019","Home Phone":"--","Work Phone":"--","Mobile Phone":"--","Pager":"--","Other Phone":"--","Email":null,"Sales Type":"Pre-Need","Note 1":"This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. This is Note 3. "}]

Here is the $.ajax script:

<script type='text/javascript' >
    $(document).ready(function(){ 
        var results = <?php   echo($json_results);?>;
        $("#xls_download_button").click(function(e) {
            e.preventDefault();
            $.ajax({
                type:  'post', 
                cache:  false,
                url:  './libs/common/test.php',
                data: results
            });                 
        });
    });
</script>

This is the test.php file:

<?php 
    echo 'Jason Data: ' . $_POST['data'];
    $results = json_decode($_POST['data']);
    // do something with results ...
?>

When the #xls_download_button is clicked, the post event fires but no data is passed in the post (viewed in firebug).

As an end result the only thing that I am trying to do is to pass the array data to an export_to_excel.php file (currently substituting test.php in place of for troubleshooting) the export_to_excel.php creates anexcel file and then returns.

Can you see what is wrong here, or perhaps a better way to approach passing this data to the PHP page?

3 Answers 3

1

Thats because you need to pass your data.

<script type='text/javascript' >
  $(document).ready(function(){ 
    var results = <?php   echo($json_results);?>;
    $("#xls_download_button").click(function(e) {
      e.preventDefault();
      $.ajax({
        type:  'post',
        data: 'data=' + results            <========
        , cache:  false
        , url:  './libs/common/test.php' 
      });                   
    });
  });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

And if you need to access it from $_POST["data"] it will be: data: {data: results}
Ok I did this and know firebug shows post data but I don't see any data in the success handler: test.php <?php $results = json_decode($_POST['data']); return $results; // do something with results ... ?> And script like: $.ajax({ type: 'post' , cache: false , data: 'data='+results , url: './libs/common/test.php' , success: function(resp) { alert(resp); } }); The alert in success shows blank.
1

I don't see where you give jQuery the post data

  $(document).ready(function(){ 
    var results = <?php   echo($json_results);?>;
    $("#xls_download_button").click(function(e) {
      e.preventDefault();
      $.ajax({
        type:  'post'
        , data: {'json_results':json_results}
        , cache:  false
        , url:  './libs/common/test.php' 
      });                   
    });
  });

Now in test.php you will have $_POST['json_results'] with the expected value as JSON string, that need to be decoded.

9 Comments

Yes in the original post I missed putting the data line in the example. shortly after posting I edited it in... sorry and thanks for the example.
In the final functionality the data is passed to export_data_to_excel.php and it should manipulate the data into a XLS compatible format and should present a 'save' dialog. How would this $.ajax post return the 'save' dialog generating in the export_data_to_excel.php script?
Where you save your temporary data ? When you call the export_data_to_excel.php the data is parsed, but you need to save it somewhere (before asking the user to save), return the save dialog, or succes and show the save dialog from $ajax.success(), and when the user confirm you need to take tha data from temporary location and store it where you need it.
Ok Your description is great. Thank you!
I have another snag. When printing the json encoded data in test.php <?php $results = json_decode($_POST['data']); print_r($results); ?> It returns: > [object Object],[object Object],[object Object] This is cleary not the same as the output I displayed in the first post - what do you think is happening?
|
0
$.ajax({
        type:  'post', 
       cache:  false
        , url:  'http://localhost/projectname/yourfilepath_and_name.php',
        data: "result="+results,
        success: function(resp) {
          alert(resp);
        } 

      });     
//and in php
do
print_r($_POST);

1 Comment

This will not work as you have results as a JSON Object. Not a sting

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.