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?

asked Oct 1, 2013 at 20:36
7
  • Separate them with a semi-colon, not the ampersand. I'm not sure how it would work if both functions returned something different. Commented Oct 1, 2013 at 20:39
  • Which function's value do you want to return? form_check or another_script? Commented Oct 1, 2013 at 20:39
  • Why isn't another_script inside form_check function simply? Commented Oct 1, 2013 at 20:40
  • @Mike Christensen Thanks i want to execute both function.is that possible? Commented Oct 1, 2013 at 20:41
  • 1
    This may not be the issue but I think you may be confusing the bitwise & operator with the logical && operator. Commented Oct 1, 2013 at 20:44

4 Answers 4

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
}
answered Oct 1, 2013 at 20:41
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Oct 1, 2013 at 20:43

3 Comments

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
@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.
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))
0

Does this work?

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

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

answered Oct 1, 2013 at 20:42

Comments

0
onsubmit="return ((form_check(this) + another_script(this))===2);"

Demo fiddle

answered Oct 1, 2013 at 21:03

Comments

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.