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

onsubmit if checkbox is checked 1

Status
Not open for further replies.

evr72

MIS
Dec 8, 2009
265
0
0
US
Hello,

I have a script that when you click the submit button it opens outlook and sends an email. I have added a checkbox to the form, what I am trying to accomplish is if the checkbox is checked then run the open email function
here is what I have done so far
Code:
<input type="submit" onClick="openoutlook()" value="Insert Record" >

<script type=text/javascript>
<-- I added this if statement
if (document.form.rmacomplete.checked == true)


function openoutlook()
{
		
  var L=document.form.CMNumber.value;
  var S=document.form.company.value;
  var B=document.form.email.value;
  var M=document.form.credupdatedby.value;
  var A= <% = Cdbl((rsGuestbook("ID")))%>
  var url = '[URL unfurl="true"]http://mywebsite.com/a1/view.asp?id='[/URL] + escape(A);

  
 self.location="mailto:someemail@mycompany.com?subject=?Record Number  "+A+" Has Been Updated by "+M+" &body= Here is the link to the updated Record   "+url+"   The Reference is "+L ;
}
</script>

but my if statement does not seem to be working, I also tried to add the onclick to the submit
 
That's not valid syntax. You can't Condition the definition of a function.

Why: because the function needs to exist for it to be called. And even then since your If has no curly braces, it only affects the line immediately after it. Your definition is 2 or 3 lines further down which means it is unaffected.

If that actually worked you'd get an error because your script would be trying to call an undefined function.

You can do 2 things here:

1. Place the condition inside the function so you Condition the actions it takes.

Code:
function openoutlook()
{
 
[red]       
if (document.form.rmacomplete.checked == true)
{
[/red]
  var L=document.form.CMNumber.value;
  var S=document.form.company.value;
  var B=document.form.email.value;
  var M=document.form.credupdatedby.value;
  var A= <% = Cdbl((rsGuestbook("ID")))%>
  var url = '[URL unfurl="true"]http://mywebsite.com/a1/view.asp?id='[/URL] + escape(A);

  
 self.location="mailto:someemail@mycompany.com?subject=?Record Number  "+A+" Has Been Updated by "+M+" &body= Here is the link to the updated Record   "+url+"   The Reference is "+L ;
[red]}[/red]

}

Or

2. Condition the calling of the function.

Code:
<input type="submit" onClick="[red]if (document.form.rmacomplete.checked == true){[/red]openoutlook();[red]}[/red]" value="Insert Record" >













----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ohhh wo I spent almost all morning trying to figure this one out!!!
Vacunita!!!!! You rule!!! as always... :)

Thank you, I opted to go with the
Code:
<input type="submit" onClick="if (document.form.rmacomplete.checked == true){openoutlook();}" value="Insert Record" >

I tried to do something similar but was missing the {} brackets
Again, Thank you!!!!
 
You can't Condition the definition of a function.

Yes you can, given the function is just another type... e.g

Code:
a = 1;
b = 1;
if (a == b) {
   wibble = function() {
      alert('a == b');
   }
} else {
   wibble = function() {
      alert('a != b');
   }
}
wibble();

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
O.K. I stand corrected. You can condition the definition of a function.
With that said the rest of my post remains valid.

That is without an alternate definition for the function you get an error when trying to call it.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Phil said:
You can condition the definition of a function.
With that said the rest of my post remains valid.
Yes, excepting this :
Phil said:
And even then since your If has no curly braces, it only affects the line immediately after it. Your definition is 2 or 3 lines further down which means it is unaffected.
[tt]if[/tt] affects the next one instruction. That one instruction can be in the same line or after pages of nothing. ( Yes, one instruction. A block is actually one instruction containing others. )

So Dan's code will work with abit of changes like this :
JavaScript:
a [teal]=[/teal] [purple]1[/purple][teal];[/teal]
b [teal]=[/teal] [purple]1[/purple][teal];[/teal]

[b]if[/b] [teal]([/teal]a [teal]==[/teal] b[teal])[/teal]

[gray]// empty lines and comments are ignored by the interpreter[/gray]

   wibble [teal]=[/teal] [b]function[/b][teal]()[/teal] [teal]{[/teal]
      [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'a == b'[/i][/green][teal]);[/teal]
   [teal]}[/teal]

[gray]// empty lines and comments are ignored by the interpreter[/gray]

[b]else[/b]

[gray]// empty lines and comments are ignored by the interpreter[/gray]

   wibble [teal]=[/teal] [b]function[/b][teal]()[/teal] [teal]{[/teal]
      [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'a != b'[/i][/green][teal]);[/teal]
   [teal]}[/teal]

[COLOR=darkgoldenrod]wibble[/color][teal]();[/teal]
( No, I do not suggest to write unreadable & unmaintainable code like above. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top