It is possible to perform two actions at the same time when submitting a form?
Here is my example:
<input type="checkbox" name="setM" id="m" value="1" onClick="this.form.submit();setM();" />
there is another way to submit the form and in the same time to process the function?
when i submit the form i do some process, then with the result i process the setM. so i need the for submitting will be the first.
EDIT
<script type="text/javascript">
function DoAllThese() {
One();
Two();
Three();
Four();}
I can call to this function then one will do the setM function and Two will submit the specific form?
4 Answers 4
Unless you are using AJAX any post-submit behaviors that you run will not have any effect on the page because the page will be replaced with the result of the form post. If you do use AJAX after submitting the form, even in the best case you'll have some race condition between the effects of the post-submit actions and the page being unloaded in anticipation of the new page being loaded.
Since it's not really possible from your question to determine what you are trying to do, I'll just give some general guidelines.
Run any actions that may influence form submission before submitting the form, then only submit the form once these complete. If form submission is conditional on successful completion of the functions, then make sure you prevent the default action (form submission) when they fail.
If you want to affect the current page based on the results of form submission, use AJAX and handle the form submission entirely through JavaScript with the functions that depend on the form post being run in the AJAX success callback (or on 200 OK response if you're handling the AJAX manually).
Don't apply your behaviors in mark up, but rather by attaching handlers to elements using JavaScript. This will help make your JavaScript code understandable because the behaviors will be localized in your code rather than scattered throughout the document.
Comments
as other two guys explained in a very beautiful way, i also say that what you want to do will be possible with the use of ajax for submitting the form.
<form>
<button onclick="ajaxsubmit()">Submit</button>
</form>
your ajax:
function ajaxsubmit(){
// do your other functions
$.ajax({
// ajax send
}).done(function(){
// submitted successfully
});
}
Comments
When you submit the form, the browser leaves the page, so presumably the "process" you describe is a server side one.
You can't cause JavaScript to run on the next page from an event handler on the page the browser just left.
You would need to either:
- Submit the data with XMLHttpRequest (Ajax)
- Have the next page include the JS you want to run
Comments
HTML
<form name="myform" action="test.html" onsubmit="verify()">
<!-- FIELDS -->
</form>
Javascript
function verify()
{
One();
Two();
Three();
}
onsubmitevent. However, it must be clarified: are the form submission and function call supposed to be synchronous or asynchronous? I don't think the latter can occur because the form submission would cause navigation away from the page.actionof the form set to?Two()submits thenThree()andFour()will not matter. They may execute if the post takes a long time, but they will not influence the post whatsoever.