Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Sending an Ajax request before form submit

$('form').submit(function(e) { 

     // this code prevents form from actually being submitted
     e.preventDefault();
     e.returnValue = false;

     // some validation code here: if valid, add podkres1 class

     if ($('input.podkres1').length > 0) { 
        // do nothing
     } else {

        var $form = $(this);

        // this is the important part. you want to submit
        // the form but only after the ajax call is completed
         $.ajax({ 
             type: 'post',
             url: someUrl, 
             context: $form, // context will be "this" in your handlers
             success: function() { // your success handler
             },
             error: function() { // your error handler
             },
             complete: function() { 
                // make sure that you are no longer handling the submit event; clear handler
                this.off('submit');
                // actually submit the form
                this.submit();
             }
         });
     }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: metamask event disconnect 
Javascript :: disable scroll react 
Javascript :: youtube video regex 
Javascript :: how to access form values in react 
Javascript :: mongoose findbyidandupdate return updated 
Javascript :: how to get selected value of dynamically created dropdown in jquery 
Javascript :: lexical scoping javascript 
Javascript :: angular 404 on refresh 
Javascript :: replace element from string javascript 
Javascript :: javascript print random word from list 
Javascript :: jquery get selected checkbox value array 
Javascript :: Find smallest Number from array in javascript 
Javascript :: await useeffect react 
Javascript :: express.static 
Javascript :: how to change image source using javascript 
Javascript :: input field take only number and one comma 
Javascript :: copy to clipboard jquery javascript 
Javascript :: use onchange with react select 
Javascript :: how to change attribute link in javascript 
Javascript :: jquery checkbox unchecked 
Javascript :: onloadscroll to top 
Javascript :: how to disable onclick event in javascript 
Javascript :: react-native android build apk 
Javascript :: how to get id of current element clicked 
Javascript :: reactive forms change event in angular 
Javascript :: how to stop browser back js history.pushState 
Javascript :: import react icons 
Javascript :: js document.addEventListner 
Javascript :: typeorm get data from a table by array of id 
Javascript :: javascript on keypu 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =