Hi,

By default, Ajax.Request is *asynchronous*, so that code won't wait
for one to complete before it kicks off the next, so you'll end up
with multiple outstanding calls to the server.  If you have more than
two outstanding calls, the odds are very high that the browser,
server, or both will queue them up and only handle two at a time.  The
order of that queuing is completely unspecified.  But even if it were,
the vagaries of things mean that some calls will complete more quickly
than others, making room for the next, and so in short the order of
your calls is almost completely non-deterministic.

A couple of ways to fix this:

1. Modify your server-side code so you can send it all nine pieces of
information in one request, and leave it to handle it at that end.

2. Make the order of your calls explicit, probably by using a chained-
loop mechanism where the "next" call doesn't get initiated until the
previous one completes.  More in this recent thread:

http://groups.google.com/group/prototype-scriptaculous/browse_thread/thread/8f2d1d3f13f7be3c/38fff7db726de049

3. (I only mention this to forestall someone else mentioning it.) You
can make the calls synchronous using an option to Ajax.Request.  But
really, don't do that, it locks up the UI in most browsers and makes
for an unpleasant user experience.

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Feb 22, 4:07 pm, "zjoske....@gmail.com" <zjoske....@gmail.com>
wrote:
> My form has master-detail data in it. Every master record has 9 detail
> records.
> When I store the detail information I use ajax.request in a loop to
> call it 9 times to add the detail records to mysql database.
> However, it seems that the order in which the new records are added is
> not the same as the order in which they were called.
>
> This is how my save function looks like:
>   function save()
>   {
>     for(var i=1;i<=9;i++)
>     {
>       new Ajax.Request('insert.php',
>                         {
>                           method:'get',
>                           parameters:
>                           {
>                             /* a lot of params*/,
>                             preventCache: new Date().getMilliseconds()
>                           },
>                           onSuccess: function(transport)
>                           {
>                             var data = transport.responseText;
>                             alert(data);
>                           },
>                           onFailure: function()
>                           {
>                             alert('something went wrong...');
>                           }
>                         }
>                       );
>     }
>   }
>
> The insert.php executes the insert statement with the correct params.
> Is there a way to use something like a transaction? Or do I need to
> add an extra field in the table to determine the order?
>
> Zjoske.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to