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??
The submit event fires when the user clicks a submit button ( <button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form.
The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.
5. Most HTML forms have a submit button at the bottom of the form. Once all of the fields in the form have been filled in, the user clicks on the submit button to record the form data. The standard behaviour is to gather all of the data that were entered into the form and send it to another program to be processed.
OnSubmit is used on a form , and indicates that the information is to be submitted to the server at this point unless you return false. OnClick is used on anything, and indicates that it was clicked, offering no other context to the intention of the event at all.
No you cannot execute a function after the form has been submitted - the order of which things are executed is as follows :
onclick
function is executedurl
specified in the action of the formYou 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
This is not possible because the form's action
would redirect the browser to that URL.
1 option would be to run testFun()
on your action
url
page, but this might not be possible depending on what the function does.
If you are to post more information about what you are actually trying to do here, then it might help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With