[英]Passing a jQuery array to PHP (POST)
I want to send an array to PHP (POST method) using jQuery. 我想使用jQuery将数组发送到PHP(POST方法)。
This is my code to send a POST request: 这是我发送POST请求的代码:
$.post("insert.php", { // Arrays customerID: customer, actionID: action })
This is my PHP code to read the POST data: 这是我的PHP代码,用于读取POST数据:
$variable = $_POST['customerID']; // I know this is vulnerable to SQLi
If I try to read the array passed with $.post
, I only get the first element. 如果我尝试读取$.post
传递的数组,则只会得到第一个元素。 If I inspect the POST data with Fiddler, I see that the web server answers with "500 status code". 如果我使用Fiddler检查POST数据,则会看到Web服务器以“ 500状态代码”回答。
How can I get the complete array in PHP? 如何在PHP中获得完整的数组?
Thanks for your help. 谢谢你的帮助。
To send data from JS to PHP you can use $.ajax : 要将数据从JS发送到PHP,可以使用$ .ajax:
1/ Use "POST" as type , dataType is what kind of data you want to receive as php response, the url is your php file and in data just send what you want. 1 /使用“ POST”作为类型 , dataType是要作为php响应接收的数据类型 , url是您的php文件,并且在数据中仅发送您想要的数据。
JS: JS:
var array = {
'customerID': customer,
'actionID' : action
};
$.ajax({
type: "POST",
dataType: "json",
url: "insert.php",
data:
{
"data" : array
},
success: function (response) {
// Do something if it works
},
error: function(x,e,t){
// Do something if it doesn't works
}
});
PHP: PHP:
<?php
$result['message'] = "";
$result['type'] = "";
$array = $_POST['data']; // your array
// Now you can use your array in php, for example : $array['customerID'] is equal to 'customer';
// now do what you want with your array and send back some JSON data in your JS if all is ok, for example :
$result['message'] = "All is ok !";
$result['type'] = "success";
echo json_encode($result);
Is it what you are looking for? 是您要找的东西吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.