JavaScript submit form with button outside form using ajax submit form with javascript,submit form with jquery,submit form

I try to post a form using ajax the current code does not really work. When I press the save button the form get submitted n + 1 times. i.e After refreshing the page it submit once, next time I submit two form get submitted, third time... etc.

I have spend a lot of time researching this already (2 days) and I have not found a questions quite similar to what I am asking.

I am on a steep learning curve here so I hope someone can point out to me what I am doing wrong.

I think I might have mixed something up. The steps up to submit is.

  1. Form values is being filled in.
  2. A button is pressed to show a modal to confirm to submit the form (The submit button is actually inside this modal and not inside the form itself).
  3. Form is submitted.

$('#confirmYes').click(function() {
  $('#confirm-object').modal('hide'); // close confirm modal
  $('#newForm').submit(function (e) {
    e.preventDefault();
    let formData = $(this).serialize();
    
    $.post({
      type: 'POST',
      url: '/api/pois/',
      data: formData
    })
<form id="newForm">
  <input type="text" id="name" name="name">
  <input type="text" id="company" name="company">
</form>
    
<div class="modal-footer">
  <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
  <button type="submit" class="btn btn-success" form="newForm" id="confirmYes">Save</button>
</div>
Answer:1

The issue is because you are creating a new submit event handler in every click. From the description of what you want to do, you instead need to create a single submit handler when the page loads, and trigger it when the button is clicked. Something like this:

$('#newForm').submit(function(e) { // handle the submit event
  e.preventDefault();
  let formData = $(this).serialize();

  $.post({
    type: 'POST',
    url: '/api/pois/',
    data: formData
  })
})

$('#confirmYes').click(function() {
  $('#confirm-object').modal('hide');
  $('#newForm').submit(); // trigger the submit event
});
Answer:2

I am developing a chatbot using Dialogflow, I would like to throw a message to user when the chatbot doesn't understand the user input for three times in a row and for the forth time respond with a ...

I am developing a chatbot using Dialogflow, I would like to throw a message to user when the chatbot doesn't understand the user input for three times in a row and for the forth time respond with a ...

  1. throw custom message java
  2. throw custom message c#
  3. throw custom exception message c#
  4. throw custom exception message java
  5. throw custom error message c#
  6. throw custom error message java
  7. powershell throw custom message
  8. throw exception custom message
  9. throw custom error message
  10. sql server throw custom message
  11. throw new exception custom message
  12. throw new exception custom message c#
  13. sql throw custom error message
  14. javascript throw custom error message
  15. sql throw with custom message

I have defined a function as following : function getCurrentComponent(){ if($rootRouter._currentInstruction){ return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(...

I have defined a function as following : function getCurrentComponent(){ if($rootRouter._currentInstruction){ return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(...

  1. function handle to symbolic matlab
  2. function handle to string matlab
  3. function_handle with value
  4. function handle with multiple variables matlab
  5. function handle with two variables
  6. function_handle with value matlab
  7. function handle with parameters matlab
  8. string handling function
  9. function handle for loop matlab
  10. function handle with multiple outputs
  11. handle to function matlab
  12. function handle to symbolic
  13. function handle with multiple variables
  14. function handle to double
  15. function handle with arguments matlab
  16. function handle to matrix
  17. function handle with array
  18. function handle to

The following Promise behaves as expected, the Promise in the return statement gets fulfilled and the then right afterwards gets executed before the last call of then: (new Promise(resolve => ...

The following Promise behaves as expected, the Promise in the return statement gets fulfilled and the then right afterwards gets executed before the last call of then: (new Promise(resolve => ...

Here is my code function consec(string) { for (let letter of string) { console.log(letter); } } console.log(consec("zoo")); the result is "z" "o" "o" undefined This is the link of my ...

Here is my code function consec(string) { for (let letter of string) { console.log(letter); } } console.log(consec("zoo")); the result is "z" "o" "o" undefined This is the link of my ...