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!

possible to put an "if" statement on submit form button? 1

Status
Not open for further replies.

orangejuice

Technical User
Nov 18, 2002
33
0
0
CA
I have created a simple form for site visitors to subscribe to a couple of different newsletters. The form is being submitted to my email address - I find this simpler than a cgi script or something. I have 3 checkboxes in the form for three different newsletters. When the email is sent, I want the subject line to change according to what checkboxes were checked. If one and two were checked, for example, the subject=Subscribe one, Subscribe two. Is it possible to do this? If so - how?? Any help is appreciated!
 
<script>
function subForm(){
numChecked = 0
for (x=1; x<=3; x++){
isChecked = eval(&quot;document.myForm.myCheck&quot; + x + &quot;.checked&quot;)
if (isChecked){
numChecked ++
}
}
document.myForm.action = &quot;mailTo: myEmail@mydomain.com?subject=&quot; + numChecked + &quot; magazine subscriptions&quot;
document.myForm.submit()
}
</script>
<form name=&quot;myForm&quot;>
<input type=checkbox name=&quot;myCheck1&quot;>
<input type=checkbox name=&quot;myCheck2>
<input type=checkbox name=&quot;myCheck3>
<input type=button value=&quot;Submit&quot; onClick=&quot;subForm()&quot;>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
That is awesome! Thanks so much... One question though, in the email I get, the subject line is &quot;1 Subscriptions&quot;, &quot;2 Subscriptions&quot;, or &quot;3 Subscriptions&quot;, depending on whether I check one, two, or all three of the boxes. Any way I can have it say &quot;myCheck1 Subscriptions&quot; or &quot;myCheck3 Subscriptions&quot;, referencing the specific box rather than just the amount of boxes checked? If that makes any sense...
 
It is all possible, but I am not sure what exactly you are asking. Is this it?

function subForm(){
magString = &quot;&quot;
for (x=0; x<document.myForm.elements.length; x++){
if (document.myForm.elements[x].type == &quot;checkbox&quot;){
if (document.myForm.elements[x].checked){
magString += &quot;, &quot; + document.myForm.elements[x].name
}
}
}
if (magString.length > 0){
magString = magString.substr(2) //peel off first comma
document.myForm.action = &quot;mailTo: myEmail@mydomain.com?subject=Magazine subscriptions - &quot; + magString
document.myForm.submit()
}
else{
alert(&quot;Please select at lease one magazine.&quot;)
}
}
</script>
<form name=&quot;myForm&quot;>
<input type=checkbox name=&quot;Time&quot;>
<input type=checkbox name=&quot;NewsDay&quot;>
<input type=checkbox name=&quot;PC Mag&quot;>
<input type=button value=&quot;Submit&quot; onClick=&quot;subForm()&quot;>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
You are brilliant! That is exactly what I wanted... THANKS SO MUCH YOU HAVE SAVED MY BUTT ON THIS ONE!! I am not a programmer - and this is exactly what I was looking for!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top