您好我一直在嘗試從jquery傳遞一個包含地理位置數據的對象數組到一個PHP腳本以保存到數據庫。一直試圖讓這個工作,所以可能會缺少一些睡眠不足的東西。從jquery到php傳遞一個對象數組並保存到數據庫
jQuery的對象stucture如下
var testData = [];
var coords = {
lat: 12.6544885,
lng: 23.545665
};
var pos = {
timestamp: 1222355465,
latlng: coords
};
testData.push(pos);
var coords = {
lat: 55.6544885,
lng: 55.545665
};
var pos = {
timestamp: 555,
latlng: coords
};
testData.push(pos);
我試圖用通過阿賈克斯張貼此以下
$.ajax({
type: 'POST',
data: JSON.stringify(testData),
//change the url for your project
url: 'www.mydomain.com/save2.php',
success: function(data){
console.log(data);
alert('Sucess');
},
error: function(){
console.log(data);
alert('Error');
}
});
,我在PHP端進行解碼,並試圖在地方一個數據庫使用以下內容。
$myData = json_decode($_REQUEST['testData']);
$sql = "INSERT INTO walk (timestamp, latitude, longitude) ";
$sql .= "VALUES ($myData->timestamp, $myData->latlng->lat, $myData->latlng->lng)";
我會appriciate任何輸入在這個問題上謝謝。
請運行'print_r($ myData)'併發布。 –
對於我的數據的值+1,以及任何錯誤消息 – CodeMonkey
您是否嘗試過'$ myData = json_decode($ _ POST ['testData'])'? – verbumSapienti