简体   繁体   English

通过AJAX从PHP发回多个结果

[英]Sending back multiple results from PHP via AJAX

i have setup some ajax, which i am just testing now, pretty much the idea behind it is to send some data from dropdown boxes to a php script, do some calculations and then return the result, it does it well and returns the result, but now rather than just sending back one result and outputting that, i want to send back multiple results and output them, i am able to send multiple data to the php script, so i am sure i can send multiple back. 我已经设置了一些ajax,我现在正在测试,其背后的想法是将一些数据从下拉框发送到php脚本,做一些计算然后返回结果,它做得很好并返回结果,但现在,而不是只发回一个结果并输出,我想发回多个结果并输出它们,我能够发送多个数据到PHP脚本,所以我相信我可以发回多个。

Anyway it only sends the first result back and not the rest. 无论如何,它只发送第一个结果而不是其余的结果。

Here is the AJAX 这是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>   

And here is the php 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));

 ?>

You can see that i have already tried to give it a go from what i thought might work, if you need any other code or want me to remove the code i added which doesn't work, then let me know. 您可以看到我已经尝试从我认为可能有用的东西,如果您需要任何其他代码或想要我删除我添加的无效的代码,然后让我知道。

Thanks for all and any help 感谢大家和任何帮助

You're only going to receive one response object: 您只会收到一个响应对象:

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

Try this. 试试这个。 Think it will help. 认为这会有所帮助。 No need to use status codes if u gonna use only success case 如果您只使用成功案例,则无需使用状态代码

 $.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