Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:09:41+00:00 2026-06-01T11:09:41+00:00

I am developing an app in PHP and at some points the app has

  • 0

I am developing an app in PHP and at some points the app has to make multiple AJAX POSTs (which each return a small amount of HTML). Each of these POSTs has to insert the returned HTML in a div. I have no problem doing that but the problem comes when I start all the AJAX POSTs at once (let’s say I start 10), and I specify a separate div for each POST. Because they are all asyncronous and they all return at about exactly the same time all the HTML ends up in one div, instead of each separate div. I’ll post some code to clarify. Here is a stripped down version of my ‘apiCall’ function:

xhr = $.ajax({
    type: "POST",
    url: "../api.php",
    dataType: "json",
    data: data,
    success: function(data) {
      obj.html(data.D['html']);
    }
});

It’s around the success function where the problem occurs. data.D[‘html’] contains the HTML from the POST but I think its getting overwritten by the next AJAX POST so thats why all I see is the AJAX POST that returns last being put into the div (this is part of a jQuery plugin I wrote, obj is the div that was passed in) I call it like this:

$("#container).apiCall({options...});

Can anyone offer a solution to make it so that when these AJAX POSTs return I can put the returned data from that particular POST into the correct div?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:09:42+00:00Added an answer on June 1, 2026 at 11:09 am

    You are doing this several times and you always refer to the same obj.
    In the meantime, you are overwriting your obj.

    By the time all the ajax functions are set up and the responses are starting to arrive (and the callback start to run), all the obj references point to the last value of obj.

    Use a closure to preserve it in another variable (in this case, myObj):

    xhr = (function(){
      var myObj = obj;
      return $.ajax({
          type: "POST",
          url: "../api.php",
          dataType: "json",
          data: data,
          success: function(data) {
            myObj.html(data.D['html']);
          }
      });
    })();
    

    Instead of assigning what $.ajax() returns directly to xhr, it creates an anonymous function and calls it inmediately: ( function(){ /* code */ } )()

    That function “backups” obj into myObj (a variable local to the anonymous function), and then simply returns what $.ajax() returns.

    I didn’t test this code. Tell me if there is any error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a facebook app using php SDK, my app is small and
I am developing a small part of a PHP application with some python code.
So I have a simple Apache with MySql I am developing a PHP app.
I'm using the data mapper pattern in a PHP app I'm developing and have
I'm developing an internal web app on our company intranet using PHP. One section
Im developing an App which is going to be used on Samsung Galaxy SII
I'm developing a Facebook app which allow my users to draw their wanted path
I am developing an app for android where I need some data from a
I'm developing a PHP web app that handles information about certain companies. So far,
Ihave been developing an app for some time. This involves entering and deleteing alot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.