2012-10-18 17 views
0

大家好,我需要將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 
+0

那麼,是什麼問題?或者問題? – Havelock

+0

我想使用JavaScript。所以當我得到結果時,我需要使用ajax來獲取它們,然後我需要用javascript填充數組,而不是用php –

+0

我不喜歡你的'$ coordinates'的外觀:'new google.maps.LatLng ('。$ row ['Latitude']。','。$ row ['Longitude']。')'看起來像'eval'是內在的。請,請不要使用'eval' –

回答

1

好,讀的評論,它似乎是var flightPlanCoordinates應該被分配一個AJAX調用的返回值的情況下。在這種情況下:

var flightPlanCoordinates = []; 
$.ajax({type: 'POST', 
     url: 'history.php', 
     data: {'query': url}, 
     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); 
      } 
     } 
    }); 

,爲避免eval,只返回數據,如下所示:

while ($row = mysqli_fetch_assoc($result)) 
{ 
    $coordinates[] = array('x' => $row['Latitude'], 'y' => $row['Longitude']); 
} 
echo json_encode($coordinates);//send as JSON object 

這應該做的伎倆

+0

你是怎麼解決的所以我不需要使用implode? –

+0

這非常簡單:通過AJAX調用請求數據:JS已經被髮送到客戶端,所以你不能在代碼中添加任何東西。相反,您請求數據並將其發送[JSON編碼](http://www.php.net/json_encode)。 jQuery會爲你解析這個問題,並且給你一個對象數組,以便......只在'success'函數中使用,請記住你。在這個函數中,你可以進一步處理數據,在這種情況下:實例化新的對象。 –

+0

我得到這個錯誤:'未捕獲的類型錯誤:無法在此行'設置屬性'0'未定義'flightPlanCoordinates [i] = new google.maps.LatLng(data [i] .x,data [i] .y) ;'我在數組 –

4

你見過:

$result = dbMySql::Exec($query); 
while ($row = mysql_fetch_assoc($result)) 
{ 
    $coordinates[] = array('x' => $row['Latitude'], 'y' => $row['Longitude']); 
} 
echo json_encode($coordinates);//send as JSON object 

,我得到在控制檯中的錯誤的類型json_encode?這應該對你有所幫助。

-1
var flightPlanCoordinates = <?php echo json_encode($coordinates); ?> 
-1
<script> 
    var coordinates = new Array(); 
    <?php 
     foreach($coordinates as $key => $value) { 
    ?> 
     coordinates[<?php echo $key; ?>] = [<?php echo $value; ?>]; 
    <?php 
     } 
    ?> 
</script> 

這從PHP傳遞數組的JavaScript

+0

不要使用'new Array' +這裏的問題是_AJAX_,你的回答在異步情況下不起作用 –

相關問題