0

I have an array that is looping and each one of them needs to go through a ajax/php function. However this doesn't seem to be working. Can you show me a correct way of doing this?

for (var i = 0; i < arr.length; i++) {
    $.ajax({
        type: "POST",
        data: "data=" + arr[i],
        url: "proxy.php",
        success: function(msg){
            $("#test").html(msg+msg);
        }
    });
}

The id "test" needs to contain all the vals gotten from php.

3
  • Kevin B Please respond so i can accept it! Thanks Commented Nov 2, 2012 at 20:26
  • 1
    One thing to be aware of here, the A in AJAX stands for asynchronous. What this means for you is that the responses from your server might not arrive in the order you sent them out. Commented Nov 2, 2012 at 20:34
  • Another issue is, depending on the length of your array, you could be sending out a whole bunch of requests at once, which can also cause a problem on the server. You might want to think about how to throttle your requests. Commented Nov 2, 2012 at 20:44

1 Answer 1

1

.html() replaces the current content with what you pass into it. Instead, you need to add to the content.

$("#test").append(msg,"<br />");
Sign up to request clarification or add additional context in comments.

2 Comments

Could be write $("#test").append(msg,'<br />') to fit jquery typo
Ah, yeah i forgot .append could take multiple arguments. Thanks!

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.