Ofir Attia
Ofir Attia

Reputation: 1275

Submit Form And Call Function At The Same Time

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?

Upvotes: 0

Views: 3007

Answers (4)

doniyor
doniyor

Reputation: 37846

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
 });
}

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532435

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.

Upvotes: 2

Adriano Silva
Adriano Silva

Reputation: 2576

HTML

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

Javascript

function verify()
{
    One();
    Two();
    Three();
}

Upvotes: 1

Quentin
Quentin

Reputation: 943185

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

Upvotes: 1

Related Questions