[英]Passing a jQuery array to PHP (POST)
我想使用jQuery將數組發送到PHP(POST方法)。
這是我發送POST請求的代碼:
$.post("insert.php", { // Arrays customerID: customer, actionID: action })
這是我的PHP代碼,用於讀取POST數據:
$variable = $_POST['customerID']; // I know this is vulnerable to SQLi
如果我嘗試讀取$.post
傳遞的數組,則只會得到第一個元素。 如果我使用Fiddler檢查POST數據,則會看到Web服務器以“ 500狀態代碼”回答。
如何在PHP中獲得完整的數組?
謝謝你的幫助。
要將數據從JS發送到PHP,可以使用$ .ajax:
1 /使用“ POST”作為類型 , dataType是要作為php響應接收的數據類型 , url是您的php文件,並且在數據中僅發送您想要的數據。
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
$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);
是您要找的東西嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.