form.send vs form.submit and files

a place to get help

Moderator: 1.2 Moderators

Forum rules
  • Every time you need help create a new thread (but please use the search function before, as your problem maybe has already been solved)
  • use code highlighting (help)
  • After your problem has been solved you have to rename the subject to "[solved] <Topicname>" AND set the Topic icon as solved (the green check)

form.send vs form.submit and files

Postby lax4mike on Wed Mar 10, 2010 12:05 am

I'm trying to upload a file. I have a form with an <input type=file> inside.
  1. <form id='myform' enctype="multipart/form-data" action="file_upload.php" method='post' target='upload_target'>
  2.     <input type="file" name="uploaded_file" value="" id="uploaded_file" />
  3. </form>


this is what i originally tried:
  1. $('myform').set('send', {
  2.     onSuccess: function(){
  3.         console.log('great success!');
  4.     }
  5. });
  6.  
  7. $('myform').send();

That code works great... for any <input> EXCEPT <input type=file>.

I found that I have to use

This code works fine to upload the file, but now I don't have the onSuccess event handler.

How can I get the best of both worlds??
lax4mike
mootools connoisseurs
 
Posts: 49
Joined: Fri Dec 12, 2008 8:19 pm

Re: form.send vs form.submit and files

Postby wowenkho on Wed Mar 10, 2010 1:41 pm

somehow, i'm also having problems with making ajax work when it comes to file uploading. just so you know, form.send() is a shortcut method for using Ajax to send the form. form.submit() is already available in vanilla JS as a means to submit the form normally (w/o the user of ajax) without having to click the submit button of the form. form.submit() isn't a MooTools-originated method and therefore, won't necessarily know onSuccess() unlike form.send() where in .send() is actually a part of the MooTools Request class.

i know there are workarounds for this existing elsewhere, we simply need to google it up. i'm gonna do some investigating myself.
my sword is yours...
http://wowenkho.com

for those who want to talk with me quicker, add me on Yahoo! Messenger through my ID: michaelapeles
User avatar
wowenkho
mootools freak
 
Posts: 1146
Joined: Sat Aug 15, 2009 10:38 am
Location: Antipolo City, Philippines

Re: form.send vs form.submit and files

Postby lax4mike on Wed Mar 10, 2010 4:22 pm

Yeah, submit is part of JS, not mootools. Why doesn't send do the same thing as submit? ie. handle enctype="multipart/form-data"?

on a side note... I'm able to use submit with the form ajaxy style by directing the output of the call to a hidden iframe. Note the target='upload_target' in the form. upload_target is just a hidden iframe. This allows the user to submit the form, while not leaving the page:

  1. <iframe name="upload_target" style="display: none "></iframe>


maybe we can add some sort of listener to the iframe?
lax4mike
mootools connoisseurs
 
Posts: 49
Joined: Fri Dec 12, 2008 8:19 pm

Re: form.send vs form.submit and files

Postby phal0r on Mon Mar 15, 2010 11:20 pm

As far as i know the XmlHttpRequest Object which is the core of every ajax call only supports get and post as methods, but for file transfer is the put method necessary. You already found a proper solution with that hidden iframe and to have event handling it should be possible to listen at the onLoad event of the iframe and i guess one parameter of the callback function is the content of the iframe, so you could send json data.
phal0r
mootools connoisseurs
 
Posts: 33
Joined: Thu Nov 20, 2008 5:10 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest