Google Answers Logo
View Question
 
Q: Multiple onSubmit Functions ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Multiple onSubmit Functions
Category: Computers > Programming
Asked by: therhino-ga
List Price: $20.00
Posted: 22 May 2006 08:17 PDT
Expires: 21 Jun 2006 08:17 PDT
Question ID: 731289
I need to add to functions to a form upon submission:

1) I need to make sure the checkbox is ticked

2) If the checkbox is ticked and the form is submitted successfully, I
need to disable the submit button to prevent multiple submissions.

Here is example code which can be used for the above requirements individually:


Checkbox checker
----------------
<!--
function checkCheckBox(hh){
if (hh.accept.checked == false )
{
alert('You must accept our Privacy Policy to continue');
return false;
}else
return true;
}
//-->

<form action="---" method="post" enctype="multipart/form-data"
onsubmit="return checkCheckBox(this)">
<input type="checkbox" value="0" name="accept">




Disable submit button code
--------------------------


<!-- Begin
function disableForm(theform) {
if (document.all || document.getElementById) {
for (i = 0; i < theform.length; i++) {
var tempobj = theform.elements[i];
if (tempobj.type.toLowerCase() == "submit" ||
tempobj.type.toLowerCase() == "reset")
tempobj.disabled = true;
}
}
}
//  End -->


<form action="---" method="post" enctype="multipart/form-data"
onsubmit="return disableForm(this);">




So, I need a piece of code which combines these 2 functions please.
Answer  
Subject: Re: Multiple onSubmit Functions
Answered By: palitoy-ga on 22 May 2006 09:30 PDT
Rated:5 out of 5 stars
 
Hello therhino-ga,

Thank-you for your question.

The easiest way to disable an element on a page is to make sure
everything is *named* in the form, by doing this you can then directly
reference them.

Lets take this form:

<form name="form1" action="---" method="post"
enctype="multipart/form-data" onsubmit="return checkCheckBox(this)">
<input type="checkbox" value="0" name="accept">
<input name="submitbtn" type="submit" />
</form>

This form is called "form1" and has a checkbox called "accept" and a
submit button called "submitbtn".

Let's look at this piece of javascript:

document.form1.submitbtn.disabled=true;

This code would disable an element called "submitbtn" in the form
called "form1" on the page.

To combine this with your example we would be left with an example page like this:

<script language="javascript">
function checkCheckBox(hh){
if (hh.accept.checked == false )
{
  alert('You must accept our Privacy Policy to continue');
  return false;
}
else
  document.form1.submitbtn.disabled=true;
  // remove the following line to remove the alert!
  alert("The Submit button is disabled!");

  return true;
}
//-->
</script>
<form name="form1" action="---" method="post"
enctype="multipart/form-data" onsubmit="return checkCheckBox(this)">
<input type="checkbox" value="0" name="accept">
<input name="submitbtn" type="submit" />
</form>

If you require any further assistance with this matter please do not
hesitate to ask for clarification and I will do my best to respond
swiftly.
therhino-ga rated this answer:5 out of 5 stars
Spot on, thanks

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy