我正在尝试将此数组从javascript发送到PHP,但我一直在第3行获取Undefined index:list。此错误来自php文件。
我将提供包括html,javascript和php在内的整个代码。
这是HTML代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type= "text/javascript" src = "js.js"></script>
</head>
<body>
<form class = "wall" action="phpf.php" method="post">
<label>What is your name</label>
<input type="text" name ="name" />
<input type = "submit" value = "Submit Order" />
</form>
</body>
这是Javascript代码
$(document).ready(function () {
var list = [34, 56, 23, 90, 43, 58];
$.ajax({
type: "POST",
url: "phpf.php",
data: {'list': JSON.stringify(list)},
cache: false,
success: function (response) {
console.log("This is the list", list);
}
});
})
这是我在PHP中收到它的地方
<?php
$list = json_decode($_POST['list']);
echo "This is the new list".$list;
?>
我想要有人能帮我。
答案 0 :(得分:0)
您无法使用echo打印数组,您应该在php文件中使用print或var_dump:
<?php
$list = json_decode($_POST['list']);
echo "This is the new list";
print_r($list);
我已经验证并且它正在工作,我只是添加jquery,我想你正在js.js中加载文件。但我把我的代码放在这里:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type= "text/javascript" src = "https://code.jquery.com/jquery-2.2.4.js"></script>
</head>
<body>
<form class = "wall" action="phpf.php" method="post">
<label>What is your name</label>
<input type="text" name ="name" />
<input type = "submit" value = "Submit Order" />
</form>
</body>
<script>
$(document).ready(function() {
var list = [34, 56, 23, 90, 43, 58];
$.ajax({
type: "POST",
url: "phpf.php",
data: {'list': JSON.stringify(list)},
cache: false,
success: function (response) {
console.log("This is the list", list);
}
});
});
</script>
</html>