5

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.

Anyway it only sends the first result back and not the rest.

Here is the 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 

$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

3
  • Why do you need to do addition and multiplication in PHP? JS is perfectly capable
    – Martin
    Jul 21, 2012 at 16:14
  • i am just using it as a testing part to get the ajax to work first, the php script is going to be pretty big, so i am doing that after Jul 21, 2012 at 16:17
  • ok just checking ;) I see you've got it solved anyway :)
    – Martin
    Jul 21, 2012 at 17:01

2 Answers 2

7

You're only going to receive one response object:

function (response) {
    $("#expected_gain").html(response.value);
    $("#house_price").html(response.value2);
}
2
  • brilliant, that works perfectly, will this work if i want to send back 6 different results? Jul 21, 2012 at 16:19
  • It will work more or less the same: if you want to send back, for example, array( "width" => 10, "height" => 5, "depth" => 3 ), you'll be able to use response.width, response.height, and response.depth. Jul 21, 2012 at 16:21
4

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);
        }
    });
1
  • Good catch with the success instead of the status code. +1!
    – Linuxios
    Jul 21, 2012 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.