2016-02-13 123 views
-6

我想從ajax獲得更多的值只有一個php文件。

我的代碼如下所示:

$.ajax({ 
    type: "GET", 
    url: "phpfile.php", 
    success: function (value1,value2,value3,value4,value5,value6) { 
     $("#lga").html('<img src="' + value1 + '" alt="image" width="115" height="115"></src>'); 
     $("#lg").text(value2); 
     $("#lgi").text(value3); 
     $("#lgv").text(value4); 
     $("#lgc").text(value5); 
     $("#lt").text(value6); 
    } 
}); 

在PHP我有6 echo

<?php 
    $game = fetchinfo("value","info","name","current_game"); 
    $lastwinner = fetchinfo("userid","games","id",$game-1); 
    $winneravatar = fetchinfo("avatar","users","steamid",$lastwinner); 
    $chance = fetchinfo("percent","games","id",$game-1); 
    $items = fetchinfo("itemsnum","games","id",$game-1); 
    $ticket = fetchinfo("winticket","games","id",$game-1); 
    $value = round(fetchinfo("cost","games","id",$game-1),2); 
    $winnername = fetchinfo("winner","games","id",$game-1); 
    echo $winneravatar; 
    echo $game-1; 
    echo $items; 
    echo $value; 
    echo $chance; 
    echo round($ticket*100,7); 
    echo $winnername; 
?> 
+0

......然後?請提供更多關於您的實際PHP代碼和您想要的結果的信息。 – fusion3k

+0

函數只能正確獲取第一個值。我試圖從一個PHP中獲取更多的值,而不是擁有6個PHP文件。 – Daniel

+0

請顯示您的php代碼 – fusion3k

回答

2

你爲什麼不通過數組與AJAX? 像在php代碼中創建一個數組,並將其以編碼形式傳遞,例如。

echo json_encode($arrResult); 

比在html形式再解析它與parseJSON()

例如。 Ajax調用供您參考

$.ajax({ 
     type: "POST", 
     url: "phpfile.php", 
     }).done(function(msg) { 
      //alert(msg); 
      msg = $.trim(msg); 
      if(msg != '[]'){ 
       var obj = jQuery.parseJSON(msg); 

       $("#lga").html(''); 
       $("#lg").text(obj.value2); 
       $("#lgi").text(obj.value3); 
       $("#lgv").text(obj.value4); 
       $("#lgc").text(obj.value5); 
       $("#lt").text(obj.value6); 
     }// if 

}); 
在phpfile.php

,你的陣列應該是

$returnArray[value2] = 'abc'; 
$returnArray[value3] = '234'; 
$returnArray[value4] = 'xyz'; 
$returnArray[value5] = 'pqr'; 
$returnArray[value6] = '987'; 

echo json_encode($returnArray);