0

I have a form when the form is submitted i'll call one javascript function and action it to to form handler.

<form action="subscribe.php" onsubmit="return form_check(this)" method="post">
<input type="text" value="">
<input type="submit" value="click">
</form>

Everything Works fine, Now i want to send the same form fields to another javascript function so i tried like this on submit code

    <form action="subscribe.php" 
onsubmit="return (form_check(this) & another_script(this))" method="post">

Added & in onsubmit but the function is not triggering when the button is clicked. I think this is due to the action="subscribe.php" in the form.

I want to send the form value to another javascript function also. how can i achieve this?

rram
  • 2,004
  • 5
  • 24
  • 37

4 Answers4

1

With jQuery you can catch the form submit. The submit event is sent to an element when the user is attempting to submit a form.

HTML

<form id="myForm">...</form>

jQuery

$("#myForm").submit(function(event){
  event.preventDefault();

  var this = something;

  myFunctionA(this);
  myFunctionB(this);
});

var myFunctionA = function(this){
  // Function operation
}

var myFunctionB = function(this){
  // Function operation
}
kunalbhat
  • 1,709
  • 10
  • 11
1

There's a few answers depending on what you're trying to do.

If you want to simply run both functions and not return anything, you can simply do:

onsubmit="form_check(this); another_script(this);"

If you want to always run another_script but return the value of form_check, you can do:

onsubmit="another_script(this); return form_check(this);"

If you want to only run another_script if form_check returns true (and return true if both form_check and another_script return true), you can do:

onsubmit="return form_check(this) && another_script(this);"

However, putting too much code in an onsubmit handler is usually discouraged. You should bind event handlers after using the DOM or jQuery.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Your last example is equivalent to `return (form_check(this) && another_script(this))` since `&&` is a short circuit operator it will not execute `another_script(this)` unless `from_check(this)` returns `true` –  Oct 01 '13 at 20:49
  • @Austin - That assumes `another_script` always returns true. It might return `undefined` which would result in different behavior. I'll make a note of this. – Mike Christensen Oct 01 '13 at 20:58
  • I would also add that if you want to run both and check both you can do `(form_check(this) & another_script(this))? true : false` or `!!(form_check(this) & another_script(this))` –  Oct 01 '13 at 21:01
0

Does this work?

onsubmit="another_script(this); return form_check(this);"

of course, it's always better to use unobtrusive event handlers.

andi
  • 6,442
  • 1
  • 18
  • 43
0
onsubmit="return ((form_check(this) + another_script(this))===2);"

Demo fiddle

Moob
  • 14,420
  • 1
  • 34
  • 47