• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

My form submit is called twice

 
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a mark-up shown below. It has many forms in one view (I have shown two only). I added a class(validateForm) on each form



Now, I hijack the form submission using below code. I used form plugin



My problem is, I see two alert message(Remarks is required!) instead of one and I notice that my submit event
is being called twice so thats why I add this code alert($(this).size()) and It alerts that the size of my
wrapped set is one.

I am really not sure where to check the cause. Any hints please?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Above code will be applicable for both the forms as both are with the class name "validateForm".

submit the particular form which you want.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just out of curiosity, why bother defining your functions as "var foo = function() { ... }" in this usecase? Obviously it'll work, it just seems like extra noise to me.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Above code will be applicable for both the forms as both are with the class name "validateForm"



Hi subhash, thanks for your interest in my question.. I actually have 10 or more forms like that and I generate them in the backend with JSP.
The number of forms depends on the number of entries in my DB.


I actually am still new to jquery, I was thinking that doing this

will fire up on only the form where the submit button was clicked. I cant reference the
form with an ID since the number of the form might vary. Is my approach wrong or is my understanding not right regarding attaching this submit event?

why bother defining your functions as "var foo = function() { ... }



Hi David, I just learned javascript last month and is still in the learning phase.. I read the book Murach Javascript in order to learn javascript and
the book is doing a lot of these so maybe I got caught up in making it a habit.

I actually don't understand it thoroughly but as per my little knowledge of javascript declaring 'named' (I am not sure if this is the right term) function adds the function to the context of the window object so I am using the var foo thing.. Maybe I am wrong though..
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
function foo() { ... } is shorthand for var foo = function() { ... }

But neither method is creating functions in the global scope, since you're inside a function--they're local to the function.
 
subhash kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try to find out the parent of the submit button that is the form and then form id

you can take the reference from following code:


pass this form id to ajax code.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

subhash kumar wrote:you can take the reference from following code:


What a mess!!! Using jQuery no such machinations are necessary.

And I don't care what some book says, the following format for defining functions:

is a mess.

I point out code such as this in my own books to discuss how functions are first-class objects, but code should never actually be written like that. It's just confusing and necessary.

With regards to the double submit problem, it's usually caused by a redundant call to the native .submit() method of a form when a submit is already in progress. I don't see anything like that in your code, but it's so fragmented that it's rather hard to read.

If you comment out the ajaxForm() call, does the form still submit? If so, normally, or via Ajax?
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But neither method is creating functions in the global scope, since you're inside a function--they're local to the function



Hi David, this is +1 to my knowledge. Thank you!

Hi Bear,

Here's my second attempt to making this form clearer. I stripped out the HTML into this.



I have re-written my script to be like this. (..minus the messy part )

If you comment out the ajaxForm() call, does the form still submit? If so, normally, or via Ajax?


I actually dont have ajaxForm() but ajaxSubmit(). And I notice that the form was never submitted either normally or by ajax.

I added this in my code.

And I was surprised to find out that there was two alerts of "I was clicked!".
I am thinking that I really am missing something basic here...
I surely would beg to hear any comments from the guru's please as I have been thinking about this for sometime....

Thanks!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I *think* it probably has to do with the fact that your selector is based on the class which is the same on both forms. If you want them to submit separately you'll need to setup different event handlers for each.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In order for me to test this one, I created this simple markup to check if it submits twice. I had the notion at first that this is form plugin problem.


To my surprise It did'nt alert twice.

I scan all link javascript file and found the culprit.



Hijacking the submit button click was never a good idea I think. And I was thinking that creating a lot of $(document).ready(function(){}
*sometimes* adds complexity in debugging but it does presents a good way of managing your code. I learned in this thread that jquery does fire
up all registered event handlers and not only the first one that was declared..

Hi subhash, I notice that jquery simplifies everything and no need for making special script markup.

Also, referencing forms by class is really never a bad idea especially if there are many forms in one JSP.
Thank you guys! I did learn a lot in this thread from me being clumsy and a newbie at the same time!
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic