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

Emailing with Java Script 2

Status
Not open for further replies.

Terpsfan

Programmer
Dec 8, 2000
954
US
Is there a way of taking entries in the text boxes on an html form and inserting their contents into the email through java script. This works but with hard-coded material. Thanks

Code:
<html>
<head>
<TITLE>
</TITLE>
</HEAD>
<Body>
<FOrm NAME = "frmTest">
<INPUT TYPE = "text" Name="txtLname" Size = "25">
<a href="mailto:anyone@aol.com
?cc=anyone@aol.com
&subject=1234567890
&body=
....+....1....+....2....+....3....+....4....+....5
....+....6....+....7....+....8....+....9....+...10%0D%0A
....+....1....+....2....+....3....+....4....+....5
....+....6....+....7....+....8....+....9....+...10%0D%0A
....+....1....+....2....+....3....+....4....+....5
....+....   6....+....7....*....%0D%0A">
Email this report
</a>

</FORM>
</body>
</html>
 
To keep the same format - i.e. having an <a> tag for the user to click - build the same structure using javascript, adding the form entries, et al, and update the <a> tag href component with the new data. Alternatively, firing an window.location.href = ... from a button is equally feasible, and doesn't involve updating an existing tag.
 
Thanks for your response. I don't know java script very well, so could you provide a short code example? Thank you very much.
 
Code:
<form name="frmTest">
<input type="text" name="txtLname" size="25" />
<input type="button" value="Email" onclick="window.location.href='mailto:anyone@aol.com?cc=anyone@aol.com&subject=1234567890&body=' + this.form.txtLname.value;" />
<form>
...should do what you're asking for.
 
Ok, next question. I want to insert a new line in the email body. How would I do that? Thanks again.
 
Unfortunately this isn't working. What am I doing wrong?
Code:
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<form name="frmTest">
<input type="text" name="txtLname" size="25" />
<input type="text" name="txtFname" size="25" />
<input type="button" value="Email" onclick="window.location.href='mailto:anyone@aol.com?cc=anyone@aol.com&subject=1234567890&body=' + this.form.txtLname.value + '\n' + this.form.txtFname.value;" />
</form>
</BODY>

</HTML>
 
Oh, dear, brain offline again. URI's can't take '\n' in them, of course. Send '%0D' instead.
 
On that note, you may need to check the encoding for the fields as well - it's easy to type in characters that can screw it up rather badly. There's a generic method, 'escape()' that does this - so in fact, a better solution is:
Code:
onclick="window.location.href='mailto:' + escape('anyone@aol.com?cc=anyone@aol.com&subject=1234567890&body=' + this.form.txtLname.value + '\n' + this.form.txtFname.value);"
Note that the 'mailto:' cannot be escaped because of the colon :)
 
Ok, since you have been so gracious to answer my questions so far, I have another one for you. And directly after this I am getting a book on Java Script.

I have about 25 check boxes on the form, how could I loop through each one, and if one is checked, I would add the check box name to the email body.

Thanks again!
 
You'd be wanting something along the lines of:
Code:
onclick="window.location.href=generate_email(this);"
...
function generate_email(e) {
 var i, t;

 t = 'anyone@aol.com?cc=anyone@aol.com&subject=1234567890&body=';
 if (e && e.form && e.form.elements) {
  for (i in e.form.elements) {
   if (e.form.elements[i] && e.form.elements[i].type && e.form.elements[i].name && e.form.elements[i].value) {
    t += e.form.elements[i].type + ': ' + e.form.elements[i].name + ' = ' + e.form.elements[i].value + '\n';
   }
  }
 }
 return 'mailto:' + escape(t);
}
This iterates every form element with a type, a name and a value; you can restrict this to just checkboxes, for example, by requiring that the type equals 'checkbox'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top