大家好,我需要將PHP的結果返回給Javascript。如何在此示例中使用javascript代替PHP
這是我的PHP代碼,我需要將它形成在javascript中使用。
$result = dbMySql::Exec('SELECT Latitude,Longitude FROM data');
while ($row = mysqli_fetch_assoc($result))
$coordinates[] = 'new google.maps.LatLng(' . $row['Latitude'] . ', ' . $row['Longitude'] . ')';
我需要返回一個數組$座標和破滅就喜歡這個,但是用javascript:
var flightPlanCoordinates = [<?php echo implode(',', $coordinates) ?>];
這是我在javascript是如何開始的:
$.ajax({
type: 'POST',
url: 'history.php',
data: {'query': url},
});
var flightPlanCoordinates = [<?php echo implode(',', $coordinates) ?>];
,我需要使用返回的數據填充此變量,並使用JOIN或與PHP中的implode執行相同操作的其他函數填充此變量。
編輯:
這是我的Ajax代碼:
$.ajax({
type: 'POST',
url: 'history.php',
data: {
'id_user':$('#select-choice-user').val(),
'reg_id':$('#select-choice-reg').val(),
'd_year1':$('#select-choice-year1').val(),
'd_month1': $('#select-choice-month1').val(),
'd_day1':$('#select-choice-day1').val(),
'd_year2':$('#select-choice-year2').val(),
'd_month2': $('#select-choice-month2').val(),
'd_day2':$('#select-choice-day2').val()
},
success: function(data)//callback to be executed when the response has been received
{
for (var i=0;i<data.length;i++)
{
flightPlanCoordinates[i] = new google.maps.LatLng(data[i].x,data[i].y);
}
}
});
這些值返回JSON格式:
[
{
"x": "46.5564266666667",
"y": "15.6467166666667"
},
{
"x": "46.5566583333333",
"y": "15.6465533333333"
},
{
"x": "46.5567416666667",
"y": "15.6465566666667"
},
{
"x": "46.556745",
"y": "15.646555"
},
{
"x": "46.5567366666667",
"y": "15.6465766666667"
},
{
"x": "46.55675",
"y": "15.6465933333333"
},
{
"x": "46.55677",
"y": "15.6466116666667"
},
{
"x": "46.5567766666667",
"y": "15.6466183333333"
},
{
"x": "46.5567783333333",
"y": "15.64662"
},
{
"x": "46.5567583333333",
"y": "15.6466066666667"
},
{
"x": "46.556725",
"y": "15.6465966666667"
},
{
"x": "46.5566983333333",
"y": "15.6465983333333"
}
]
我在JSON驗證檢查和格式是有效的。所以我不知道什麼是錯的。
我會把PHP代碼只是爲了確保它被寫,因爲它應該是:
Uncaught Error: Invalid value for constructor parameter 0: undefined
Uncaught TypeError: Cannot set property '0' of undefined
那麼,是什麼問題?或者問題? – Havelock
我想使用JavaScript。所以當我得到結果時,我需要使用ajax來獲取它們,然後我需要用javascript填充數組,而不是用php –
我不喜歡你的'$ coordinates'的外觀:'new google.maps.LatLng ('。$ row ['Latitude']。','。$ row ['Longitude']。')'看起來像'eval'是內在的。請,請不要使用'eval' –