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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two onSubmit values....possible? 1

Status
Not open for further replies.

zeuseason

Technical User
Nov 1, 2003
18
US
I have a javascript form that I would like to perform two functions. Is it possible to have a form with two onSubmit values? My experience shows the form will only perform the first onSubmit and not the second. Any suggestions?
 
Why do you need to call two functions? how about the first function call the 2nd function upon completion. Or create a 3rd function which call the 1st and 2nd function.
 
My first onsubmit evaluates 3 fields for completion which executes every time being first. My second is disabling the submit button to prevent multiple submits. How can I have the first onsubmit call the second upon completion? I have the first onsubmit using the return handler to halt if false, but if true the script seems to skip over the next onsubmit directly to the action function. Maybe it is just the syntax I am using, or I am limited in the code which I doubt is the case. Any help is appreciated!

syntax------->
<form name=&quot;feedback&quot; method=&quot;POST&quot; onsubmit=&quot;return formCheck(this);&quot; onsubmit=&quot;submitonce(this)&quot; action=&quot;mailto:info@eurolinesalons.com&quot;>



 
How about in your formCheck, to have a logic like this:

Code:
funtion formCheck(form) {
  var sucess = true;
  // validate form fields here
  // change sucess to false if validation failed.
  if (sucess) {
     // disable the submit button. e.g. form.submit.disable = ture;
     return true;
  } else 
     return false
  }
}

then you don't need the submitonce() function.
 
Ok you lost me a bit. That code would be run in the script itself and not the <form> html correct? I don't why that couldn't be done though. Once the script runs, it can disable the button and continue to the action function. I shall give it a shot if we are both on the same 'page'!
 
The script run after submit button is clicked. So if the submit button is disable after this point, the form can still be processed continue to action. I tried this code and it works:


<script language=&quot;javascript&quot;>

function formCheck(form) {
var ok = true;
if (form.check.checked != true)
ok = false;

if (ok) {
form.submit.disabled = true;
return true;
} else {
return false;
}
}
</script>
<form name=&quot;feedback&quot; method=&quot;POST&quot; onsubmit=&quot;return formCheck(this);&quot; action=&quot;mailto:info@eurolinesalons.com&quot;>
<input type=&quot;checkbox&quot; name=check value=&quot;true&quot;/>test
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;/>
</form>
 
Much thanks! I will give it a try right away!
 
This code works exactly as onsubmit however the expression evaluates the checkbox and now I just need the 3 fields evaluated. Your help definitely puts me on the right track and I should be able to tweak it from here. I have the field names so I should be able to input them into the 'if' statement I think.
 
For the if statement:

if (form.check.checked !=true) is testing the 'check' named in the input script for a 'checked' then I can input 'if (form.name.value !=true) and use this statement for the 3 fields I need to evaluate?
 
let me know what the field name and type are, and how do you want them to be validated (non empty field etc...), I shall be able to come up something for you.
 
I have 3 fields: name, phone, email
I'm really not looking for much in validation. I'd be happy if the field was evaluated as non-empty. If this script evaluates these fields, doesn't that eliminate the formcheck(this) script? I'm a novice when it comes to this stuff but trying to learn!
 
here you go. I added couple line of code so that it hide the form after user sucessfully submit the form, and show a thank you message.

Note that I change your formCheck script as follows:

Code:
<script language=&quot;javascript&quot;>

function formCheck(form) {
  var ok = true;
  if (trim(form.name.value) == &quot;&quot;) 
    ok = false; 
  if (trim(form.phone.value) == &quot;&quot;)
    ok = false;    
  if (trim(form.email.value) == &quot;&quot;)
    ok = false;    

  if (ok) {
     form.submit.disabled = true;
     // comment out the following two lines if you want to preseve the form after submittion
     document.getElementById('feedbackForm').style.display='none';
     document.getElementById('thankyou').style.display='block';
     return true;
  } else {
     alert('Please complete all required field');
     return false;
  }
}

function trim(s) {
  return s.replace( /^\s*/, &quot;&quot; ).replace( /\s*$/, &quot;&quot; );
}
</script>


<div id='feedbackForm'>
<form name=&quot;feedback&quot; method=&quot;POST&quot; onsubmit=&quot;return formCheck(this);&quot; action=&quot;mailto:info@eurolinesalons.com&quot;>
Name: <input type=&quot;text&quot; name=&quot;name&quot; /><br />
Phone: <input type=&quot;text&quot; name=&quot;phone&quot; /><br />
email: <input type=&quot;text&quot; name=&quot;email&quot; /><br />
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;/>
</form>
</div>

<!-- show this message after the form is submitted -->
<div id='thankyou' style=&quot;display:none&quot;>
Thank you for your feedback.
</div>
 
Wow, very good. This script works verywell. Thanks for all of your help and if you need and graphics help (my forte) send me an e-mail! Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top