0

I use a webservice to get availability of hotels, but cannot get ajax to show results of more than one. My method is definitely flawed & slow - how better to do?

1) Code (simplified) called for EACH hotel in a page listing 6-12 hotels:-

<script>
$(document).ready(function(){
    $("#div<%=i%>").load("hotel-avail.php?id=xxyy");
});
</script>

2) hotel-avail.php?id=xxyy uses a webservice to get a PHP array, $result, defining availability of the hotel with id xxyy. Content of the html section:-

<script>
var result = JSON.parse( '<?php echo json_encode($result) ?>' );
    if (result.hotels.estado == 'OK') {
        document.getElementById("demo").innerHTML = "Yes";
    } else { 
        document.getElementById("demo").innerHTML = "No";
    }
</script>

Availability: <span id="demo"></span>

So this method injects data twice into 2 divs, one in each file. Apart from looking bad, and NOT working, it must be the slowest way to iterate through 6-12 hotels. Help please!

1
  • Not clear where second script tag is... is that the response from hotel-avail.php? If so that output makes no sense, would only output the actual json and manipulate it into html in ajax callback Commented Apr 24, 2016 at 0:05

2 Answers 2

1

Use this code as reference, If I am missing the point, let me know

 $.ajax({
    url: 'hotel-avail.php?id=xxyy',
    method: 'post',
    dataType: 'json',
    success: function (result) {

        if (result.hotels.estado == 'OK') {
            document.getElementById("demo").innerHTML = "Yes";
        } else {
            document.getElementById("demo").innerHTML = "No";
        }
    }
});

This ajax only works with jQuery. forgot to point that out. also you can change this code:

document.getElementById("demo").innerHTML = "Yes";

for

$("#demo").html("Yes");
Sign up to request clarification or add additional context in comments.

4 Comments

Looks promising, does not work as yet after adjusting url. Since this processes the json in the ajax code above (in the hotel listing page), presumably html of hotel-avail.php needs changing? How to make sure ajax gets the json array from that page?
the dataType: 'json' will parse the json output from php, so result will be already parsed in the javascript, i suppose PHP is doing the following echo json_encode($result)
Now have this, is 'result' the json array created in hotel-avail.php? success: function (result) { if (result.hotels.estado == 'OK') { $("#div0").html("Yes"); } else { $("#div0").html("No"); } Will keep trying to get this version working as looks on right track.
Yes result will be the result from each url hotel-avail.php you execute
0

In case useful for others I detail final result. Following ideas of @david-lavieri, this code worked for checking hotel availability one by one via a loop and entering value into div0, div1 etc:-

    $.ajax({
    type: "GET",
    url: 'hotel-avail.php', 
    data:'querystring defining data',
    success: function(data){
      $("#div<%=i%>").html(data);
    }
    });

To get to work I removed the whole script for 2) hotel-avail.php (see my initial entry above) and replaced by 'echo json_encode($result);' at the end of the php coding calling the webservice.

This checks availability one by one. It is probably much slower than getting all the availabities in one webservice call and then updating each div. Next project!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.