簡體   English   中英

通過AJAX從PHP發回多個結果

[英]Sending back multiple results from PHP via AJAX

我已經設置了一些ajax,我現在正在測試,其背后的想法是將一些數據從下拉框發送到php腳本,做一些計算然后返回結果,它做得很好並返回結果,但現在,而不是只發回一個結果並輸出,我想發回多個結果並輸出它們,我能夠發送多個數據到PHP腳本,所以我相信我可以發回多個。

無論如何,它只發送第一個結果而不是其余的結果。

這是AJAX

 <script>
$("document").ready(function (){ 

    $(".add_extension").change(function(){


        var m = document.getElementById('meter_square');
        var meter_square = m.options[m.selectedIndex].value;

        var s = document.getElementById('story_height');
        var story_height = s.options[s.selectedIndex].value;

     $.ajax({
            type: "GET",
            url: "script.php",
            data: { meter_square: meter_square, story_height: story_height },
            dataType: "json",
            statusCode: {
                200: function (result, result2)
                {
                    $("#expected_gain").html(result.value);
                $("#house_price").html(result2.value2);
                }

            }
        });
})
});
</script>   

這是PHP腳本

    <?php 

$meter_square = $_GET["meter_square"];
$story_height = $_GET["story_height"];


$result = $meter_square + $story_height;
$result2 = $meter_square * $story_height;

echo json_encode(array("value" => $result, "value2" => $result2));

 ?>

您可以看到我已經嘗試從我認為可能有用的東西,如果您需要任何其他代碼或想要我刪除我添加的無效的代碼,然后讓我知道。

感謝大家和任何幫助

您只會收到一個響應對象:

function (response) {
    $("#expected_gain").html(response.value);
    $("#house_price").html(response.value2);
}

試試這個。 認為這會有所幫助。 如果您只使用成功案例,則無需使用狀態代碼

 $.ajax({
        type: "GET",
        url: "script.php",
        data: { meter_square: meter_square, story_height: story_height },
        dataType: "json",
        success: function(data){
            $("#expected_gain").html(data.value);
            $("#house_price").html(data.value2);
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM