0

I'm not sure if this question has been asked before, so please note if it's duplicate.

The top line is a PHP merged array that I'm trying to send in AJAX to another page.

I'm having trouble finding what goes in the "?" or how to format it.

<?php $_SESSION['pdfData'] = array_merge_recursive($count, $qty); ?>

$.ajax({ 
	type: "POST",
	url: "indexMenu-sm-pdf.php",
	data: { 
            ? 
        },
	dataType: "JSON", 
	success: function(data){
	    console.log("Good.");
	},
	    error: function(jqXHR, textStatus, errorThrown) {
		console.log(textStatus, errorThrown);
	} 
});

I understand that the built-in function json_encode() but i lack the know-how. And I've been reading about serialization. Is one better than the other?

My main questions are:
What goes into the "?" as $_Session['pdfData'] can not be sent.
And
Is there a difference is using json_encode vs. serialization for this issue?

4
  • 1
    indexMenu-sm-pdf.php could just read the data from the session Commented Feb 12, 2018 at 19:53
  • The meta header the company is using is preventing the session from being passed. While I wish it was that easy... this is was the next step I thought of (albeit, much more difficult). Commented Feb 12, 2018 at 19:56
  • what meta header? that's a new one. Commented Feb 12, 2018 at 19:58
  • Typically, because PHP is NOT a single page application you'd share data between pages via session or data fetches. Is there a specific reason you want to AJAX data between pages directly without any PHP code in between for data handling? Commented Feb 12, 2018 at 20:01

2 Answers 2

4

It should be something like this:

$.ajax({ 
    type: "POST",
    url: "indexMenu-sm-pdf.php",
    data: { 
            paramName: <?php echo json_encode($_SESSION['pdfData']) ?>
        },
    dataType: "JSON", 
    success: function(data){
        console.log("Good.");
    },
        error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    } 
});

Replace paramName with the name of the $_POST parameter that indexMenu-sm-pdf.php is expecting to get the PDF data from. json_encode() will output the contents of the session variable in the form of a Javascript literal.

Sign up to request clarification or add additional context in comments.

Comments

0

The answer from Barmar is right. But you can also split the php and the js code. Set a DOM element in your page with the php value and access it via js within your ajax call.

See the following example:

1.) Set the DOM element on your page

<div id="jsDomElementPdf" style="display:none">
  <?php echo json_encode($_SESSION['pdfData']); ?>
</div>

2.) Access the value in your script

let paramName = document.getElementById("jsDomElementPdf") || '';

if(paramName){
   $.ajax({ 
     type: "POST",
     url: "indexMenu-sm-pdf.php",
     data: { 
        paramName: paramName
     },
     dataType: "JSON", 
     success: function(data){
       console.log("Good.");
      },
     error: function(jqXHR, textStatus, errorThrown) {
     console.log(textStatus, errorThrown);
    } 
  });
}

Comments

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.