0

So, i have this simple script where i try to stop form submision for make some ajax call and after i want to submit form manually, but this doesn't work, I try over 20 solutions. I try event to stop form submit with prevent default and fire latter in callback as i see in some example but for me give recursion

<script type="text/javascript"> $(function() { $('#my-form-submit').click(function(e) { e.preventDefault(); $('#my-form').submit(function() { alert('test'); }); }); }); </script> <form method="post" id="my-form" action='#'> <input type="submit" name="my-submit" value="Submit this form!" id="my-form-submit"> </form> 
3
  • 5
    You are not submitting the form, you are binding a submit event listener, just like how you bound a click event to the button. $('#my-form').submit() would submit it. Commented Mar 1, 2018 at 17:11
  • Can you please also add the ajax calls that you want to execute before submitting the form? And as @epascarello mentioned form submition would be just $('#my-form').submit() Commented Mar 1, 2018 at 17:12
  • Submit function without callback dont refresh and post page in a normal way. Doesn t matter what ajax call i make because ajax it s ok. But after the normal post with refreshing page to send result directly to php doesn t happen Commented Mar 2, 2018 at 10:12

2 Answers 2

1

You attached a event listener for when the form will submited.

To submit the form, just pass the method submit() without any parameter, or like so:

$('#my-form').submit(function() { alert('test'); }).submit(); 
Sign up to request clarification or add additional context in comments.

1 Comment

I will try this. But this will pake page refresh as i havent any javascript?
0

If u want to capture event before send a form, you only need to use .submit()

 $(function() { $('#my-form').submit(function() { alert('Check it is showed before submit test'); }); }); 

2 Comments

Of i do that the ajax call don t happend because o think the form is submitting fast and make page refresh
Try it then. $(document).ready(function() { $("form").submit(function(e){ alert('submit intercepted'); e.preventDefault(e); }); });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.