Submit a form in the background

C

Christoph

I have an HTML form that I would like to submit without reloading the
page. I know how to go about this technically - attach a function to
the submit button's onclick event that builds an HttpRequest, sends it
and blocks the form from submitting in the normal fashion.

But building the request body from the form elements is a real bother.
So far most of these forms have had only one or two fields which were
easily stuck together manually, but now I have a file upload field
that complicates things.

Is there some kind of document.getElementById('formID').buildRequest()
function that builds the request the way the browser normally would,
but returns it as a string instead of sending it to the server? It's
hard to believe that all Ajax forms out there cobble together a query
string from the values of each of their individual fields...
 
V

VK

but now I have a file upload field
that complicates things.

It doesn't complicate: it simply eliminates the XmlHttpRequest out of
options - it has no access to type=file fields so it's not able to
submit them. So use more universal and reliable way with hidden target
iframe as already suggested.

For form w/o type=file fields you still can use XmlHttpRequest-based
libraries like from www.ajaxtoolbox.com - they can handle forms of any
complexity.
 
I

Ian Hobson

Christoph said:
I have an HTML form that I would like to submit without reloading the
page. I know how to go about this technically - attach a function to
the submit button's onclick event that builds an HttpRequest, sends it
and blocks the form from submitting in the normal fashion.

But building the request body from the form elements is a real bother.
So far most of these forms have had only one or two fields which were
easily stuck together manually, but now I have a file upload field
that complicates things.

Is there some kind of document.getElementById('formID').buildRequest()
function that builds the request the way the browser normally would,
but returns it as a string instead of sending it to the server? It's
hard to believe that all Ajax forms out there cobble together a query
string from the values of each of their individual fields...

Your solution is to have the script that handles your post return a 200
reply with no content.

This will leave the old page displaying on the screen.

NB - Only tested in IE6 and Firefox.

Regards

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top