2011-09-26 50 views
2

我正在嘗試使用此代碼調用php文件,訪問數據庫,檢索值並使用JSON對象返回它們,然後將它們編輯爲文本框。我的代碼有什麼問題(使用dojo xhrGet Ajax)

代碼爲Javascript結束:

當用戶改變下拉列表的選項,應用程序應該調用PHP腳本從數據庫中獲取新的價值,從一個JSON對象檢索它們,並修改文本區域顯示新的值。

<select id="busSelect" name="busSelect"> 
    <option>S053-HS - P</option> 
    <option>S059-HS - P</option> 
    <option>S064-HS - P</option> 
    <option>S069-HS - P</option> 
    <option>S070-HS - P</option> 
</select> 

    <textarea id="memo"></textarea> 




    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js" type="text/javascript"></script> 
    <script type ="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 

    <script type ="text/javascript"> 
     <?php 

     ?> 
     dojo.ready(function(){ 
      var myOptions = { 
       zoom: 12, 
       center: new google.maps.LatLng(26.4615832697227,-80.07325172424316), 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 

      var map = new google.maps.Map(dojo.byId("map_canvas"), 
      myOptions); 

      dojo.connect(busSelect,"onchange",function(){ 

       dojo.xhrGet({ 

        url: "getRoute.php", 
        handleAs: "json", 
        timeout: 1000, 
        content: { 
        route: dojo.byId("busSelect").value 
        }, 

        load: function(result) { 

         var formResult = result.lat_json + " " + result.long_json + " " + result.name_json; 
         dojo.byId(memo).value = formResult; 

        } 

       }); 

      }); 

php腳本:

應該把它從JS應用程序,這是「總線名稱」接收到的名字,並使用該名稱查找總線ID。運行時,14:那麼它應該訪問總線使用ID(這所有的作品,我只是得到JSON/AJAX位錯)

<?php 

header('Content-type: application/json'); 

    require_once 'database.php'; 

    mysql_connect($server, $user, $pw); 
    mysql_select_db("busapp") or die(mysql_error()); 

    $route = $_GET["route"]; 

    $result_id = mysql_query("SELECT * FROM routes WHERE name = $route"); 

    $result_row = mysql_fetch_array($result_id); 
    $route_id = $row['id']; 

    $result = mysql_query("SELECT * FROM stops_routes WHERE route_id = $route_id") 
    or die(mysql_error()); 




    $markers; 
    $stopid = array(); 
    $time = array(); 
    $lat; 
    $long; 
    $name; 
    $waypts = array(); 


    for ($x = 0; $row = mysql_fetch_array($result); $x++) { 
     $stopid[$x] = $row['stop_id']; 
     $time[$x] = $row['time']; 
    } 

    for ($x = 0; $x < sizeof($stopid); $x++) { 

     $result = mysql_query("SELECT * FROM stops WHERE id = $stopid[$x]") 
     or die(mysql_error()); 

     $row = mysql_fetch_array($result) 
     or die(mysql_error()); 

     $lat[$x] = $row['lat']; 
     $long[$x] = $row['long']; 
     $name[$x] = $row['name']; 

    } 

    $size = count($stopid); 

    $lat_json = json_encode($lat); 
    $long_json = json_encode($long); 
    $name_json = json_encode($name); 

?> 

我也越來越上dojo.xd.js錯誤停止。

回答

0

取代單個變量傳遞給json_encode(),您應該創建一個對象,然後將其編碼爲JSON,然後僅使用正確的Content-type標頭回顯JSON。

// Start with an associative array 
$arr = array("lat_json" => $lat, "long_json" => $long, "name_json" => $name); 
// Cast it to an Object of stdClass 
$obj = (object)$arr; 

// Encode it 
$json = json_encode($obj); 

// And return it to the calling AJAX by just echoing out the JSON 
header("Content-type: application/json"); 
echo $json; 
exit(); 

之前編碼JSON,你的對象現在看起來像(我的示例數據):

stdClass Object 
(
    [lat_json] => 12345 
    [long_json] => 45678 
    [name_json] => the name 
) 

// After json_encode() 
{"lat":12345,"long":45678,"name":"the name"} 

正如您所設置的接收端的JavaScript,我相信它應該不加修改地運行。爲了確定JavaScript端的JSON結構,請務必在您的load()函數中檢查console.dir(result)