Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Darn that Netscape! It's always som

Status
Not open for further replies.

trojan800

Programmer
Nov 27, 2001
53
US
Darn that Netscape! It's always something. However I have a feeling this may be a very quick and easy answer for you experts and gurus. Any and all answers/suggestions are greatly appreciated. Ok, so I have two functions that I want to perform client-side when the user clicks submit (onsubmit). The scripts are correct b/c when executed by themselves in either IE or NS their fine... The problem is when I run them together, just in NS of course, only the first script runs, ignoring the second. Maybe its as simple as the way I referece them. Please let me know, thanks. The code is as follows:

<form action=&quot;form.asp&quot; method=&quot;post&quot; name=&quot;FORM1&quot; onSubmit=&quot;return ActionOne() ; return ActionTwo()&quot;>
 
Just a suggestion: Have you tried removing the return statements and adding the script call to 'javascript:'?

Try this:

<form action=&quot;form.asp&quot; method=&quot;post&quot; name=&quot;FORM1&quot; onSubmit=&quot;javascript:ActionOne();ActionTwo()&quot;>

 
Thanks TWillard for the suggestion. However it didn't like that either, it is now ignoring both functions. Please keep em coming though. Thanks.
 
TWillard... Just kidding, you are the man. I had a small typo. So now it recognizes and executes both functions however, both functions need to have the ability to return false if something isn't done (i.e. verifing data entered) as you have it, it runs through both functions automatically. Does that make sense? I hope so. Anyway sorry about the last post.
 
I think it has something to do with the way that you are referencing the form to submit: For example, the following test code works fine in IE5 and NS6, (I only tested for IE5 and NS6 by the way).

TEST CODE - COPY AND PASTE -- AND IT WORKS!
-------------------------------------------
<script language=&quot;javascript&quot;>

function go1(){
alert('go1');
}

function go2(){
alert('go2');
}
</script>

<form action=&quot;test2.html&quot; method=&quot;post&quot; name=&quot;FORM1&quot;
onSubmit=&quot;javascript:go1();go2();&quot;>
<input type=submit>
</form>


trojan800,

You may have your form setup correctly, but you may need to address the call to submit it. I am using an input type of submit.
 
you can only return one thing. Try this :
[tt]<form action=&quot;form.asp&quot; method=&quot;post&quot; name=&quot;FORM1&quot; onSubmit=&quot;return ActionOne() && ActionTwo()&quot;>[/tt]
That will return true if both functions return true. Otherwise it will return false.

 
Thanks so much guys. Both work, misery347 your way is exactly what I was looking for. Thanks again.
 
Just playing devils advocate, but what if you decide you need to add a couple MORE functions to the onSubmit? I don't believe is calling multiple functions in an onSubmit for just that reason. My preference is to call a SINGLE function, and have that function call the others. Like this:
Code:
function submitter() {
  if ( ActionOne() )
     return ActionTwo();
  else
     return false;
}
Additionally, I think that Javascript uses &quot;short-circuit&quot; execution of the && (and), which means if the first function call returns false, the second function will never be called. If that's not what you want, misery's method won't work. However the function I wrote above can be modified to call both functions regardless, and return false if either of them returns false:
Code:
function submitter() {
  var r1 = ActionOne();
  var r2 = ActionTwo();
  return r1 && r2;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdragon,
You read my thoughts. You're absolutely right and I would recommend the same.

good luck
 
Personally, I would not create a whole new function just to return two other functions. Simply more syntax. But if you really want to, here's a shorter way:
Code:
<form onsubmit=&quot;return function() {return ActionOne() && ActionTwo();}&quot;>
haha... Same effect. Go with Misery's advice.
 
I still say: &quot;What do you do when you decide you also need to call ActionThree and ActionFour&quot;? I think it's neater, easier to read, and easier to maintain when the onSubmit calls only ONE function.

And you still didn't address my point about the fact that if ActionOne returns false, ActionTwo is never called. That may be what you want, but it may not be. My way is more flexible. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
why dont you call the second action from the first one?
ie:

function action1()
{ some code; action2(); }

function action2()
{ some code; }

I hope this helps,

BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com

&quot;<beer brand> is like making love in a cano...
it's <f-word + ing> close to water !!!&quot;
- Monty Python's Flying Circus, 1969/70
 
Ok... tsdragon is right.

And BobbaFet, making the first function call the second makes the developer (or somebody who has never seen the code) need to jump around more to find where to put the code when wanting to add another function. If you've never seen the code before, it's more convenient for all the functions to be called by one main function.
 
I actully agree with tsdragon. I just fixed his syntax. It is easier to maintain that way and short circut evaluation could really cause some problems. Wish I had thought of it myself.
 
Fixed my syntax? Did I make a boo-boo? (Wouldn't be the first time.)
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
OIC. You mean all you did was fix the syntax of the way he had it coded instead of suggesting a better way to do it. I should have figured that out for myself. I'm one of those people who don't hestitate to go ahead suggest a better way (or at least what I think is a better way), instead of just answering the immediate question. I suppose it's a character flaw. ;-) Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Wow, thanks for everyones input. And as it turns out I did go your way Tsdragon. It is overall the best way. Again thanks everyone for all the help. I love this forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top