views:

123

answers:

3

This is a technical design question more then a syntax question.

I have a large page with 9 forms corresponding to different tables. I've wrestled with the design and I there's no way out I have to send all the forms via ajax to the server for processing. There are lots of interrelations. I can't combine the forms into one large one either. So the question is what's the best way to send a lot of forms via ajax. To further complicate the issue there are dynamic forms with fields with same names.

I'm trying a technique of:
1. serializing each form, 2. prepending the form name to each field name 3. combining the serialized version of the forms into one 4. posting that combined serialized form to the server as one 5. breaking them apart on the server side into separate arrays and then finally doing the application logic

I just don't know if there's a tried and true easier solution and I'm making a mountain out of a mole hill

+1  A: 

I'd prepare a javascript dispatcher which would smartly do the job of posting the data. So when the submit button is pressed it would collect all the data needed, and then send the data to the appropriate controllers on the server side. It could block the form, in the meanwhile, or display a "Processing..." popup.

Palantir
Isn't that what he said he was doing?
T.J. Crowder
+2  A: 

If there's genuinely no way to redesign the page, then your solution seems simple and straightforward to me, not at all "mountain"-ish. To me, though, your description of the page screams "redesign," though of course I don't have enough information to work with.

One such redesign would be to send field changes to the server as they happen, rather than waiting and submitting the entire thing. The server side can hold them as "pending" if you need the user to explicitly commit the whole thing when they're done. But that depends on expense of server resources, etc.

T.J. Crowder
It is going to be a decision between those 2 techniques - All at once, or piecemeal. Each has it's application complications. It's similar to an order entry system with header and detail dependencies, and the details have their own dependencies. thanks for your thoughts
sdfor
+2  A: 

You should be able to send 9 separate AJAX requests without hassle (assuming that a: each doesn't rely on the response of another, and b: this isn't something which happens all the time).

Using your javascript library (you are using one, right??) just loop through your forms and AJAX submit each one. I think it'll probably only process probably 2 at a time, but if that's not a problem to your design, then all should be sweet.

It would certainly keep the PHP/Server-Side part of the equation much much simpler.

If you were working with a high-traffic site, then you'd probably want to reduce the number of requests being made, but chances are your current setup will work sufficiently well.

nickf
Note to the OP on this: If you *do* send nine requests, be aware that after the first couple they will queue up and happen in series, not in parallel, even if you send them asynchronously. Most browsers, and many servers, limit concurrent requests between the same endpoints (two at a time, typically). So it may take some time.
T.J. Crowder
This brings up the point of dependencies. When the updates have to happen serially, at least some of them, you can't send them all at once but have to wait until one completes, use it's results before sending the second. That would increase the total time. I'll have to choose between constant updating of each form and keeping the data in a "pending" mode until the user "saves" as you recommended T.J. or combine all the forms into one submit and have one server program parse and deal with it.thanks to everyone!
sdfor