Javascript array to PHP with json.stringify


Javascript array to PHP with json.stringify

我有一个div数组是js:

[div#heaolu, div#edu, div#usk, div#lootus, div#heaenergia]

我需要把它传递给PHP。

.JS:

function send(){
            console.log(sendarray);
            JSON.stringify(sendarray);
}

.PHP:

$posts = json_decode($_POST['sendarray']);

print_r($posts); 
echo json_last_error();

print_r不返回任何内容,json_last_error在火狐中返回 0

在 chrome 中,它抛出了我:未捕获的类型错误:将循环结构转换为 JSON

您的输入不是有效的 javascript 对象,您必须将元素包装在 " 中。另外,看看 Desai @Rahul答案,你需要使用 ajax 将数据发送到服务器。

["div#heaolu", "div#edu", "div#usk", "div#lootus", "div#heaenergia"]

你需要使用 AJAX 将数据从 Javascript 传递到 PHP。

阿贾克斯 |多核

Javascript AJAX PHP 教程 |谷歌

为了将数据发送到服务器,您可以使用 JQuery.post()。