Welcome to WebmasterWorld Guest from 54.145.85.22

Forum Moderators: open

Message Too Old, No Replies

Submiting things to Email using Javascript

     
12:28 am on Jan 15, 2006 (gmt 0)

New User

10+ Year Member

joined:Jan 15, 2006
posts:22
votes: 0


New poster here *waves*

Ok, well, I am creating an iq test website and need some help on how to get the alert message with the score to come up, with it emailing the score to me.

Here's some of the source code for the quiz:
In the header...


<script language="JavaScript">
function tester() {
var a = 0
var b = 0
var c = 0
var d = 0
if(document.forms[0].elements[1].checked==true) {
a = 1
}
if(document.forms[0].elements[4].checked==true) {
b = 1
}
if(document.forms[0].elements[11].checked==true) {
c = 1
}
total = a + b + c + d
score = 100*total
if (total==0){
alert("Your score was 0...were you even trying?")
}
if (total > 0){
alert("Your score is "+ score +"! Great Job!")
}
}
</script>

and in the body, here is what the question setup looks like...

<form method="get" onsubmit="tester()">
1. Which number doesn't belong?
<p><input type="radio" name="R1" value="V1">714 </p>
<p><input type="radio" name="R1" value="V2">892 </p>
<p><input type="radio" name="R1" value="V3">993 </p>
<p><input type="radio" name="R1" value="V4">698 </p>
<div align="center"><center><p><input type="submit" value="Get Score"></p>
</center></div>

Currently, this only comes up with an alert box which says the score...but is there any way to make it still do that, and also email the score to me, all by pressing the submit?

Thanks in advance.

2:34 am on Jan 16, 2006 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 31, 2003
posts:9068
votes: 4


Welcome to the forums someone788!

There is no way of sending an email with Javascript alone. Your only possible option is to use a server-side script (PHP, Perl, ASP) to email the form results to you.

2:41 pm on Jan 16, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 12, 2003
posts:1199
votes: 0


To be more specific, your <FORM> line would look something like this:

<form method=yest action=myscript.cgi>

The file "myscript.cgi" is a Perl or PHP program you wrote and uploaded to the server. That program displays the results to the user and then emails you the results. Perl and PHP are not hard to learn and it's worth learning one of them, especially if you want to do things like send emails or store info in a database.

2:46 pm on Jan 16, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 14, 2004
posts:1181
votes: 0


Here is a script demonstrating calling the email client with mailto and populating the message. Note that problems may occur if the client computer has no default email program specified.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function sendMail(_frm){
var eml="you@youraddress.com";
var bod="&body="+_frm.selOne.value+" "+_frm.txtOne.value;
var subj="?subject=Whatever you want";
location.href="mailto:"+eml+subj+bod;
}
</script>
</head>
<body>
<form action="mailto:you@youraddress.com" enctype="text/plain"
method="POST" onsubmit="sendMail(this);return false;">
<select name="selOne">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
</select>
<textarea name="txtOne"></textarea>
<input type="submit" value="Submit" />
</form>
</body>
</html>
7:36 pm on Jan 16, 2006 (gmt 0)

New User

10+ Year Member

joined:Jan 15, 2006
posts:22
votes: 0


Ah ok, thanks all :)
4:08 am on Jan 17, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 12, 2003
posts:1199
votes: 0


One downside of that is that your email address is naked on the page, and spambots will pick it up and add it to their lists.

The other downside is that Javascript-dependent solutions require that the user has Javascript turned on. Most do, but some turn it off because it's the best way to block popup windows (since popups depend on Javascript).

Also, the script just calls up a new email message, it doesn't actually send. You're relying on the user to actually click Send, and maybe they won't.

If you link your <FORM> to a PHP or Perl script then you can avoid all three downsides.