0

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?

asked Jan 17, 2013 at 17:29
4
  • You could handle the onsubmit event. 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. Commented Jan 17, 2013 at 17:31
  • What is the action of the form set to? Commented Jan 17, 2013 at 17:32
  • post, after submiting its return to the same page. its make the process and back to the same page. then i want to process the setM function Commented Jan 17, 2013 at 17:58
  • If Two() submits then Three() and Four() will not matter. They may execute if the post takes a long time, but they will not influence the post whatsoever. Commented Jan 17, 2013 at 20:10

4 Answers 4

2

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.

  1. 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.

  2. 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).

  3. 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.

answered Jan 17, 2013 at 20:52
Sign up to request clarification or add additional context in comments.

Comments

2

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
 });
}
answered Jan 17, 2013 at 21:02

Comments

1

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
answered Jan 17, 2013 at 17:33

Comments

1

HTML

<form name="myform" action="test.html" onsubmit="verify()">
 <!-- FIELDS -->
</form>

Javascript

function verify()
{
 One();
 Two();
 Three();
}
answered Jan 17, 2013 at 18:22

2 Comments

this is what i wrote. the question is if after processing function One() , function Two() can submit the specific form.
If one of these functions return true form will be sent.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.