when form submit ,the order of click function and submit

html, javascript

Solution

No you cannot execute a function after the form has been submitted - the order of which things are executed is as follows :

- User clicks the submit button

- The `onclick` function is executed

- The browser submits the page to the `url` specified in the action of the form

You can prevent the browser submitting the page by returning `false` from the onclick handler :

function myfunc() {
  // do some stuff
  return false;
}

the submit button should then be modified like this :

<input type="submit" onclick="return myfunc()"/>

If you do wish to execute a function after the form has been submitted you need to submit the form using AJAX - this doesnt cause the browser to navigate away from the page and a JavaScript function can be executed after the form has been submitted

Problem

about click and submit example below: ``` <form action="url" method="post"> <input type="text" id="input1"/> <input type="submit" value="submit" onclick="testFun()"/> </form> ``` if it is possible that function testFun run after the form's submit when we click the button to submit if the answser is no. why? how does the browser work when click the submit button?? the order is click function-> submit ? is right??

Original source