将数组从JavaScript传递给PHP然后再传回JavaScript

时间:2022-10-16 13:03:23

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.

我需要将一个数组从一个页面传递到一个PHP页面以使其将其写入文件,然后另一个页面必须在第二个页面中访问该文件和一个数组。

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

到目前为止,我有一个JavaScript数组加载了我需要的所有信息:

JavaScript code:

JavaScript代码:

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

PHP code:

PHP代码:

<?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..

至于页面拉动我不确定如何完成这个..

2 个解决方案

#1


1  

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:

最好的方法是两种方式使用正确的JSON,即你需要在发送之前对JSON进行字符串化,否则它将作为常规URL参数传递,因此:

javascript:

JavaScript的:

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

php:

PHP:

$json = file_get_contents('php://input');
$array = json_decode($json);

#2


0  

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

你确定该数组包含“数据”键吗?尝试这个

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

#1


1  

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:

最好的方法是两种方式使用正确的JSON,即你需要在发送之前对JSON进行字符串化,否则它将作为常规URL参数传递,因此:

javascript:

JavaScript的:

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

php:

PHP:

$json = file_get_contents('php://input');
$array = json_decode($json);

#2


0  

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

你确定该数组包含“数据”键吗?尝试这个

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