douchong8393 2016-02-13 11:15
浏览 17
已采纳

使用$ .ajax从php获取更多值

I am trying to get more values from just one php file with ajax.

My code looks like this:

$.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);
    }
});

In the php I have 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; 
?>
  • 写回答

1条回答 默认 最新

  • drkxgs9358 2016-02-13 11:44
    关注

    Why dont you pass array with ajax? Like make an array in php code, and pass it in encoding form, eg.

    echo json_encode($arrResult); 
    

    than in html form again parse it with parseJSON().

    eg. of ajax call for your reference

        $.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 
    
     });
    

    in phpfile.php, your array should be

    $returnArray[value2] = 'abc';
    $returnArray[value3] = '234';
    $returnArray[value4] = 'xyz';
    $returnArray[value5] = 'pqr';
    $returnArray[value6] = '987';
    
    echo json_encode($returnArray); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记