Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loop of form submit using jquery event

The code is stuck in a loop when I try to catch a form submission. The purpose is to replace a value of the form before it goes out.

$('form').submit(function(e){
   e.preventDefault();
   $.ajax({
      type: "POST",
      url: 'convert.asp',
      data: $('form').serialize(),
      success: function(response){
         $('input[name="field1"]').val(response);
         $('form').submit();
      }
   });
   return false;
});

Does anyone have any ideas?

UPDATE: I originally had it bound to a button click event and it was working but I wanted to preserve the [enter] key element of the submit button. Seeing that the code is kind of illogical, would catching a keypress be a better idea?

like image 934
greener Avatar asked Mar 21 '12 21:03

greener


People also ask

How to submit a form using jQuery click event?

To submit a form using jQuery click event, you need to detect the submit event. Let’s submit a form using click event and without using click event. You can try to run the following code to learn how to submit a form using jQuery click event −

What is the use of submit event in HTML?

This method is a shortcut for .on ( "submit", handler ) in the first variation, and .trigger ( "submit" ) in the third. The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to <form> elements.

Is there an inner submit event for a form?

@mblase75 is right here, there is no need for the inner submit. You've bound the submit event of the form to the function within so the second call calls the first again and so on. @Christoph What about it?

How does the confirm() method block the event loop?

This simply works out of the box because the confirm () method blocks the event loop, therefore the form is not submitted until the user clicks one of the two choices. Now, let’s say that we want to use a nice-looking jQuery plugin, much more appealing to the users, to display the dialog. But when we try to emulate the previous example:


2 Answers

I assume your ajax is an attempt to validate the form before its eventual submission. In that case just unbind the validation function before submitting the form.

  success: function(response){
     $('input[name="field1"]').val(response);
     // Add unbind to remove validations
     $('form').unbind().submit(); 
  }
like image 127
Robert Beuligmann Avatar answered Sep 28 '22 20:09

Robert Beuligmann


In the success you trigger another submit...

$('form').submit(function(e){
   e.preventDefault(); // redundant,  you return false in the end. <<<===
   $.ajax({
      type: "POST",
      url: 'convert.asp',
      data: $('form').serialize(),
      success: function(response){
         $('input[name="field1"]').val(response);
         $('form').submit(); // <=== delete this! <<<<=================
      }
   });
   return false;
});
like image 34
gdoron is supporting Monica Avatar answered Sep 28 '22 20:09

gdoron is supporting Monica