Bind a submit handler to the form after it is injected, then serialize its fields and send them with $.ajax(). Because the form arrived by Ajax, its submit event must be delegated or attached post-injection. Use $(document).on('submit', '#myForm', handler) or run .on('submit', ...) inside the load callback. Ordinary .submit() binding on page load misses the element entirely. This guide explains why, then gives the working patterns, thresholds, and failure modes.
The core problem is timing, not syntax. When your page's ready handler ran, the target div was empty. The form you see now exists only because a later Ajax response inserted new nodes into the DOM. Any handler you registered against a selector that matched nothing at load time is attached to nothing. Everything below follows from that single mechanism.
Event Binding Mechanics After Dynamic Injection
Direct binding calls like $('#orderForm').on('submit', fn) run at the moment of execution. jQuery queries the DOM then, finds zero matching elements, and silently attaches nothing. No error appears in the console. This silence is the most common source of confusion: the code looks correct, and it is correct, but it executed against an empty selection.
Delegated binding solves this because it moves the listener to an ancestor that always exists, typically document or a stable wrapper div. The handler sits on the ancestor and checks, at event time, whether the actual event target matches the selector. The form can be inserted minutes later and still be caught. This is the same mechanism jQuery has used since version 1.7, when .on() and .off() replaced .live(), .bind(), and .delegate().
There are three viable attachment points, each with a cost trade-off. Document-level delegation catches events for the whole page and survives repeated replacement of the form. Container-level delegation is faster per event because the browser does not bubble as far, but the container itself must persist across reloads. Direct binding inside the Ajax success callback is the fastest per event but must be re-run every time the form is reloaded.
A subtle point about .submit() versus .on('submit'): the shorthand .submit(handler) is a legacy alias that still works, but .on() is the modern API and supports delegation. If you see tutorials using .live(), they predate jQuery 1.9 and will throw errors on any current jQuery release.
Quantified Context: Numbers That Change Your Design
A delegated listener on document adds roughly one extra event dispatch per matched event, usually measured in fractions of a millisecond on modern engines. In practice the difference between direct and delegated binding is negligible for form submissions, which happen at human speed, typically one to five per minute. Optimize delegation only when you fire synthetic events in loops of thousands, as test suites sometimes do.
Payload size matters more than handler placement. A FormData object containing 12 text fields and a 2 MB image uploads fine over a typical 4G connection but feels slow on a 3G connection, where upload throughput often sits between 0.3 and 0.8 Mbps. Two megabytes at 0.5 Mbps takes about 32 seconds. Show a progress indicator above roughly 1 MB, and consider client-side image resizing for anything above 5 MB.
Default browser upload limits are effectively the server's post_max_size and upload_max_filesize settings on PHP (commonly 8 MB and 2 MB out of the box) or the reverse proxy's body limit, commonly 1 MB on some default nginx configurations. A request that exceeds these limits fails before your jQuery handler sees a response body, sometimes returning a bare 413 status. Check the server before blaming the JavaScript.
Request timeout defaults in $.ajax() are zero, meaning no client-side timeout. Connections can hang for 30 to 60 seconds before the browser gives up. Set a timeout of 10,000 to 30,000 milliseconds for interactive forms and handle the error callback explicitly.
Ajax Submission Patterns Compared
| Pattern | How binding works | Survives form reload | Best for | Main weakness |
|---|---|---|---|---|
| Delegated on document | Selector checked at event time | Yes | Dialogs, repeatable partials | Event bubbles full document depth |
| Delegated on container | Selector checked at container | Only if container persists | Persistent panels | Breaks if container is replaced |
| Direct in success callback | Attached to the new node | No, re-run required | One-time form loads | Silent miss on reload |
| $.post shorthand | Same as any Ajax call | N/A, data only | Simple key-value posts | No progress events, no upload files |
| fetch API | Native, no jQuery | Yes with delegation semantics | Modern codebases | No IE support, more verbose |
| Native requestSubmit | Triggers form validation | Yes | Accessibility-first forms | Requires feature check in old browsers |
The comparison assumes you control the page. If you load a third-party form into your div, some sites use nested forms or frames, and the inner submit event may never bubble to your document handler. Inspect the markup before assuming delegation will catch it.
Timeline: What Happens From Click to Response
| Moment | What occurs | What to verify |
|---|---|---|
| T+0 ms | User clicks submit button | Button is type="submit", not a bare div |
| T+1-5 ms | Constraint validation runs first | Required and pattern attributes are correct |
| T+5-20 ms | Submit event fires and bubbles | Delegated selector matches the form |
| T+20-30 ms | Handler calls preventDefault() | Page does not navigate away |
| T+30-80 ms | serialize() or FormData build runs | Checkboxes, selects, files included correctly |
| T+80 ms onward | XHR sent, upload progress fires | Progress handler bound before send |
| Response time + 1-30 ms | Success or error callback fires | Status code handled, not just assumed 200 |
The validation row is the professional caveat most tutorials skip. Calling .submit() programmatically skips constraint validation in the same way it skips the submit event. If you want browser validation to run, call the native form.requestSubmit() method instead. That fires the submit event and runs validation, and your delegated handler still catches it.
Edge Cases That Break the Happy Path
A file input cannot be serialized. .serialize() reads only successful form controls that have a name and are not disabled, and file inputs carry no string value. To upload a file you must build a FormData object from the form element itself and set processData and contentType to false. Without those two flags, jQuery tries to URL-encode the body and mangles it.
Multiple forms in one container break naive selectors. If the Ajax response returns two forms, $('#myForm') picks the first match by ID, but duplicate IDs are invalid HTML and the delegation selector may match either. Scope your selector to the container and a unique class.
Nested forms are illegal. Browsers may silently drop the inner form tag, so the submit event fires on the wrong element. If your partial contains a form inside a form, restructure the markup.
Disabled submit buttons and double clicks are the easiest way to create duplicate records. Disable the button and show a spinner in the beforeSend callback, re-enable it in both success and error. A gap of 500 milliseconds is enough for most users to double-click.
Red-flag warnings: Never build a delegated selector from a user-supplied string; it invites DOM-based injection. Never skip event.preventDefault(); the browser will navigate away and lose your Ajax result. Never rely on dataType: 'json' without confirming the server sends valid JSON; a trailing comma returns the error callback, not success. Never trust HTTP 200 as success; validate the response body's own status field. Never use serialize() for forms with file inputs or unchecked-sensitive checkbox arrays.
Misconception: Serialization Covers Everything
The most persistent myth is that .serialize() sends "the form." It sends a URL-encoded string of successful controls only. Disabled fields vanish, unnamed fields vanish, unchecked checkboxes vanish, and file inputs vanish. Multi-select values appear as repeated keys like interests[]=a&interests[]=b, which PHP and Express parse into arrays, but a naive server may keep only the last value.
The second myth is that dataType: 'json' converts your outgoing data to JSON. It controls only how jQuery parses the response. Outgoing serialized form data is a string with the default application/x-www-form-urlencoded content type unless you override contentType and pass an object.
The third myth is that the Ajax request inherits the form's method and action. jQuery ignores both unless you read them yourself. A form with method="post" and action="/save" will still post to the current URL with a GET unless you specify type and url in the options. Server-side CSRF tokens in hidden fields do get serialized, but the server may reject them if the request's method differs from the one the token was issued for.
Practical Checklist Before You Ship
Bind with $(document).on('submit', '#myFormSelector', function (e) {...}) inside a $(function () {...}) ready wrapper. Call e.preventDefault() as the first statement in the handler. Detect files with a form[enctype] or input check; if present, build FormData from the form element and pass processData: false, contentType: false. For plain text forms, send $(this).serialize() with type and url read from the form's attributes or explicit constants. Set timeout between 10,000 and 30,000 ms and a dataType that matches the server's actual content type. Disable the submit button in beforeSend, re-enable in the complete callback, and show a spinner while the request is open. On success, check the parsed response's own status field and replace the container only after confirming it. On error, keep the form values intact so the user does not retype them, and surface the HTTP status and any server message.
Reconcile the two timeout thresholds explicitly: for a form under 1 MB on a fast connection, 10 seconds is generous; for a form with a 5 MB image on 3G, 30 seconds is still short. If expected upload time exceeds your timeout, use xhr.upload.onprogress with FormData and set the timeout per form rather than one global value. Pick the timeout as roughly three times the expected upload time at the slowest connection you support.
Limitations and What This Approach Cannot Fix
Delegation solves the timing problem, not the server's contract. If the endpoint expects multipart encoding and the client sends URL-encoded data, the response will be a 415 or a silent parse failure. If the server returns HTML on a partial, dataType: 'json' sends you to the error callback with a parser error message, not a helpful status. Test the endpoint independently of the form binding.
CORS and same-origin rules apply equally to Ajax submissions. A form loaded from another origin into your div may be cross-origin, and the browser will block the request unless the server sends the appropriate headers. Cookie-based CSRF protections may also fail if withCredentials is not set consistently with the server's policy.
Order of operations is the last limit worth naming. If your page loads jQuery twice, or the form's partial ships a conflicting handler that calls stopImmediatePropagation(), the delegated handler can be preempted. When a submission seems to be ignored, open the network tab first and check whether the request was ever sent. If it was not, the handler ran or was blocked before it called $.ajax(). If it was sent, read the status code before reading the code again.