jQuery AJAX'ed forms
Inspired by Dustin Diaz’s AJAX screencast, I thought it was time I made a little more use of iShowU and show how easy, and quick it can be to pile on some AJAX calories to pretty much any form.
The screencast is sans-sound, really just to try to keep my out-going bandwidth down, but I’ve commented along the way so you can understand (…hopefully…) what I’m doing, and why I’m doing it.
Download the QuickTime screencast, 9MB, a little over 10 minutes
If you can’t view the screencast, try DivX Web Player for Windows or the Mac.
You can also have a look the code used in the screencast to have a play or to upgrade.
As always, let me know if you have any questions or suggestions.
Watch out for this gotcha that I just stumbled in to: if your form has an input element with the name 'action', the line url: this.action is going to grab that element, rather than the form's action. Instead, use this.getAttribute('action')
You should follow me on Twitter here I'll tweet about JavaScript, HTML 5 and other such gems (amongst usual tweet-splurges)
K, I solved all my problems all by myself. First, using ":input" to yank all the data from the form didnt cut it for me because it doesnt read checkboxs, radio buttons, or select boxes correctly. Also, just using "escape(this.value)" didn't work, because it stripped the "+" signs out. Below is how I reconfigured that part of your script:
Later I ran into a new problem, which is that your script is using GET! But we're letting users write stuff! Some of them write a whole lot, which leads to the "414 Request-URI Too Large" error. After panicking for about an hour, I realized it was very easily remedied with a single parameter in your ajax line:
Now everything is working great! I think you should consider making these permanent changes to your code, as I find that they are huge, but easily implemented, improvements.
@Todd - that's not quite right.
First up,
:inputdoes select all the input elements, but if you only want the selected elements, then this may not be for you.Second, escape isn't what you want - you want
uriEncodeComponent- this will handle the + symbols.Lastly, the Ajax hit that includes
this.actionis wrong, as it will put the input with the id of 'action' from the form, rather than the attribute.I suspect most of these issues have been caused by my own original demo, but you can see a working demonstration of all the points I've raised here:
http://jsbin.com/uholo (to edit: http://jsbin.com/uholo/edit)
I should add that the data attribute of the
$.ajaxfunction, you probably just want:Remy,
First the JS Bin is awesome. You are the man!
Second, I'd never seen uriEncodeComponent before. Thanks for that bit.
Third, this.action is working perfect for me so far - and I use all kinds of different action urls. Can you elaborate on when it will bust? I notice in my code i use this.action (normal ole jscript) and you refer to $('this').attr('action') which is JQuery code. I've been using this.action to grab the form action url for a long time. I'm inclined to believe you are right but then theres the "if it ain't broke, don't fix it" rule which even overrules advice from superior coders. Heh.
Thanks for your help, brother!
i need to get the id (?=136) from 1 page (review_image.php) and sent it to another (submitComment.php), then from there upload the database. please help me im new to this. and bin googleing for weeks now.