You can easily submit data in an html form via jQuery's $.post. But you need to do a little more to upload a file. I suggest the jQuery Form Plugin.
Here is the HTML form:
This is the Javascript file myFrom.js:
And here the cgi script "test.cgi" in Ruby
Make sure that your cgi script has the right permission, 755. Of course you would first manipulate the data instead of just mirroring it.
Here is the HTML form:
<html> <head> <title>Title</title> </head> <body> <form action="/test.cgi" enctype="multipart/form-data" method="post"> <input name="myFile" type="file" /> <div id="results"> </div> </form> </body> </html>
This is the Javascript file myFrom.js:
$(document).ready(function() { $('form').ajaxForm( { beforeSubmit: function() { $('#results').html('Submitting...'); }, success: function(data) { var $out = $('#results'); $out.html('Your results:'); $out.append('<div><pre>'+ data +'</pre></div>'); } }); })
And here the cgi script "test.cgi" in Ruby
#!/usr/bin/ruby require 'cgi' cgi = CGI.new puts cgi.header puts "Here is the file you uploaded" value = cgi['myFile'] # <== value string for 'field_name' puts value.read # <== body of value puts value.local_path # <== path to local file of value puts value.original_filename # <== original filename of value puts value.content_type
Make sure that your cgi script has the right permission, 755. Of course you would first manipulate the data instead of just mirroring it.
It's not working...
ReplyDeleteAnd why form is after html ?
hahaha let me try first.... wish it works.
ReplyDelete(^^'a...
works...!! it workss...!!!!
ReplyDeleteI used it in a jsp environment... and it works...!!
(god bless u, author). :D
Thanks for this post!!! I had to change some things up to refresh a DIV with an MVC Partial View, but this post definitely pointed me in the right direction! Thanks again!
ReplyDeleteI get this error: $("form").ajaxForm is not a function
ReplyDeleteplease help!!
Need include jquery library
DeleteImport jquery.js look at jquery.com
DeleteHow can we use this in a JSP environment? I need to know this urgently? I mean how can I submit this to a servlet and process the upload? Can someone please help?
ReplyDeleteHow i get file "myForm.js" ??
ReplyDeleteThe above codes is myForm.js.
Deleteupsss..sorry, i dont read carefully..there's an explanation about file "myForm.js"
ReplyDeleteLet me try this code first :)
Nice, thanks ... not that hard .... do you mean "advice" not "advise" in your title ?
ReplyDeleteCopy - Paste nubs, all of U
ReplyDeleteITS NOT WORKING ..
ReplyDeleteANYONE:
can you teach me how to make a virtual classroom??
or are there any sites where i can learn.
i know very basic PHP,javascript.
This is for my thesis.
Hope SOMEONE GOOD will HELP.
URGENT.. ^^,
Can you explain more what's virtual classroom??
Deletewell, but this is not ajax and in this case jquery is not necesary
ReplyDeleteit's working fine..for me <3
ReplyDeletethanks
can you upload your js file which you include in you code
ReplyDeletethanx. work fine also for me
ReplyDeleteBrilliant, thanks!
ReplyDeleteThanks Bro.... This is exactly what i want.
ReplyDeleteGreat help.. Thanks
It is not working on IE-8
ReplyDeleteI dont know anything about ruby :((
ReplyDeleteIts working fine..Thanks a lot...
ReplyDeletethanks for this tutorial, I like this and I want to practice this tutorial to my project website
ReplyDeletenice tutorial, and i will wait your next tutorial mo advance
ReplyDeleteCan't you translate the test.cgi to php?
ReplyDeleteI don't understand the buy script :/
workssss!!!!!!! only in FF -.-''
ReplyDeleteThanks for this post!!!
ReplyDeleteIt works!
Im using rails 3