0
for(var i=0; i<participantNum; i++){
    studentID = $('#txtID'+(i+1)).val();
    alert(studentID);
    //my php function call it's work normally request(php,paramiter,cb)
    request("http://localhost/lastOrientation/2_registerValidate.php","studentID="+studentID,validateID);
}

I get value from each textbox by for loop and CALL php script to validate the student id

but the result come out randomly it's not arrange by order that it increment from for loop

,by the way all result come out. I know that it is because ajax request but can you advise

me how to get the result order in the way for loop does.

Thank in advance

sorry for my language.

3
  • Could you post the output you're getting, and the expected output? Commented Aug 8, 2013 at 6:17
  • 2
    it looks like request uses ajax... if yes it is asynchronous.. that is the reason for the behaviour Commented Aug 8, 2013 at 6:17
  • 1
    Give your Ajax full code Commented Aug 8, 2013 at 6:17

3 Answers 3

2

It looks like the request method is doing an ajax request to serve the request, if yes ajax requests are asynchronous in nature means you cannot predict when the callback will get executed or which request will get completed soon. It depends on network speed as well the server speed on serving the request.

a possible solution will be is to queue the requests like

function x(i, limit){
    if(i >= limit){
        return;
    }
    studentID = $('#txtID'+(i+1)).val();
    alert(studentID);
    //my php function call it's work normally request(php,paramiter,cb)
    request("http://localhost/lastOrientation/2_registerValidate.php","studentID="+studentID,validateID).complete(function(){
        x(i + 1, participantNum)
    });
}

x(0, participantNum);

function request(){
    return $.ajax(...)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes!! Thank you for guiding the idea I have change a little bit to suit my code but this absolutely the way I want Thank @Arun P Johny
0

It is probably because of latency between your requests to server. When first requests starts it might take longer than the second one and return result only after the second, third or other requests comes back. If you really want to keep an order you either should keep some record of which request is which when it returns result or you should make your request synchronous (I would not recommend the latter option).

Comments

0

May it will solve the issue

 async   : false,

in your ajax code

1 Comment

But don't forget what this implies: Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. jQuery.ajax

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.