SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [ajax] Multiple server requests to triggered by user action

    I have an web application (PHP backend) where I want to initiate multiple requests to the same server (to different php functions/files) and asynchronously update the response on the page (the call back function is same for all the requests).

    What is the best solution for this ? Is it better to

    a) call multiple server calls from the java script file? if so Do we create multiple http objects ? can someone point me to the few examples ?

    b) call one Php service and have the php fork the multiple requests on the server side. I am not sure if this can be done multiple requests be done asynchronously on the server side ? and if so how to dynamically update teh resultant page as each request finishes?


    Any insight into other options for this scenario is also highly appreciated.

    thanks
    Dave.

  2. #2
    SitePoint Addict
    Join Date
    Oct 2006
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd submit multiple transactions using JavaScript. An XMLHttpRequest object can only work on one transaction at a time so you will need to use multiple objects.

    Just my $0.02....

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Careful. Due to the asynchronous nature of ajax and the unpredictable nature of http request/response timings you can end up with updates that don't occur in the correct order. Consider this timeline:

    - ajax 1 initiated
    - ajax 2 initiated
    - ajax 3 initiated
    - ajax 2 complete, update page
    - ajax 3 complete, update page
    - ajax 1 complete, update page

    In some cases this doesn't matter, in other cases it does. You will have to decide.

    What I have done in the past is use 1 request/response object and a buffer:

    - ajax 1 initiated
    - try to initiate ajax 2, is ajax 1 complete? No -> store request in a buffer.
    - ajax 1 complete - anything in the buffer? Yes -> initiate ajax 2
    (etc etc, hope you follow)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •