Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to pass an array from one page to a PHP page to get it to write it to a file, then another page has to access that file an array in the second page.

So far i have an array in JavaScript loaded with all the info I need:

JavaScript code:

$.ajax({
        url: 'woepanel.php',
        type: 'POST',
        dataType: "json",
        data: vars,
     });

PHP code:

<?php
$datap = array(json_decode($_POST['data']));
file_put_contents('var.txt', print_r($datap, true));
?>

As for the page pulling I'm unsure on how to accomplish this..

share|improve this question
    
Plain old echo –  u_mulder Apr 9 at 19:18
    
So what exactly is you problem passing the array to php, writing the file in php, accessing the file written by php from the second webpage or all three? –  Timigen Apr 9 at 19:21
    
If you want to store it for a little while, then reuse it with JS, you're going to have to make a second AJAX call to get the contents via PHP, JS can't read the file you saved on the server. Just a quick tip to, if you're going to just store it for later usage what you could do is save it as JSON, parse it back to jquery to decode it, you don't need to convert it to an array with what you seem to want to do. –  Sidney Liebrand Apr 9 at 19:21
    
i cant get the array to pass to php and write it to a file –  user1547167 Apr 9 at 19:40

2 Answers 2

Best way is to use proper JSON both ways, i.e. you need to stringify the JSON before sending it, as otherwise it would be passed as a regular URL param, so:

javascript:

$.ajax({
    url: 'woepanel.php',
    type: 'POST',
    dataType: "json",
    data: JSON.stringify(vars),
 });

php:

$json = file_get_contents('php://input');
$array = json_decode($json);
share|improve this answer

Are you sure that the array contains the "data"-key? Try this

$.ajax({
url: 'woepanel.php',
type: 'POST',
dataType: "json",
data: {
    "data" : vars
  },
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.