PDA

View Full Version : Grabbing Contact form values and email them using JS



skeatt
Dec 5th, 2002, 09:40 AM
Is it possible to collect a Contact forms values and email them using JS.

Like just write them to an email and send it?

chrismiceli
Dec 5th, 2002, 01:48 PM
try this.


<form name="myForm" action="mailto:">
<input type="text" name="0" value="[email protected]">
<input type="text" name="1" value="[email protected]">
</form>
<script type="text/javascript>
function getcontacts() {
for (var i = 0; i <= document.myForm.length - 1; i++) {
document.myForm.action += document.myForm.elements[i].value
document.myForm.action += ",";
}
}

skeatt
Dec 5th, 2002, 02:30 PM
chrismiceli
Thanks, I'll try it out tomorrow, been at this monitor for 14hrs

skeatt
Dec 6th, 2002, 01:26 PM
I modified it to suit my code and got mail happening in both NN and IE, but the %0A%0D doesn't give new lines in IE (i'm using IE5).

The code below gives each value a new line in NN but strings it together in IE (even if I use %0A%0D)

Must be an alternative for IE out there somewhere:confused:

function getDetails() {
var myArray = new Array()
var message = "mailto:[email protected]?SUBJECT=Web CV Response&BODY="
for (var index = 0; index <= document.formDetails.length - 1; index++) {
myArray[index] = document.formDetails.elements[index].value
}
message += "Name = " + myArray[0]+ "%0D";
message += "Company = " + myArray[1]+ "%0D";
message += "Address = " + myArray[2]+ "%0D";
message += "City/Suburb = " + myArray[3]+ "%0D";
message += "Postcode = " + myArray[4]+ "%0D";
message += "Country = " + myArray[5]+ "%0D";
location = message;
}

<form name="formDetails" method="" action="">
<br> <a href="#" onclick="getDetails();return false;">send it form onclick</a><br>

whammy
Dec 7th, 2002, 01:57 AM
There is no alternative for IE. It is just buggy, and it won't be fixed. I hate to break the news to you like this (and trust me, I know EXACTLY what you are talking about!). :(

Unfortunately the only alternative is to use a server-side language which will process your form and send an email that way.

That is the ONLY way to format an email message the way you want.

The good news is once you start using a server-side language and learn how they interact with databases, you can do just about anything imaginable. Actually the same problem you're experiencing right now was what motivated me to learn this stuff. :)

Also, look at my signature... right now my site is in a transitional state, but if you're clever you can have my form processor send the email for you. Or for a small charge (or in your case for free, since I can totally relate with your problem!), I can customize it.

It's also very easy to format an email message yourself, using PHP, ASP, and the like... if you don't depend on javascript. Because you can't.

skeatt
Dec 7th, 2002, 04:05 AM
:D :p ..whammy.. NewsBreak!! Stop all keyPresses and mouse click, give me a star... I found a solution to the infamous "No line breaks in IE5 emailto: saga" (and I'm only a beginner :o ).

I recalled when I was playing with cookies that the %0D%0A was what you get if you don't escape things, so after a bit of fiddling (and some quesswork) I came up with this..(and it works for both NN and IE5!) by using the conditional test.

IE5 NEEDS the escape :cool:

function getDetails() {
var myArray = new Array()
var newline = document.all?escape("%0D%0A"): "%0D%0A";
var message = "mailto:[email protected]?SUBJECT=Web CV Response&BODY="
for (var index = 0; index <= document.formDetails.length - 1; index++) {
myArray[index] = document.formDetails.elements[index].value
}
message += " Name = " + myArray[0]+ newline;
message += " Company = " + myArray[1]+ newline;
message += " Address = " + myArray[2]+ newline;
message += " City/Suburb = " + myArray[3]+ newline;
message += " Postcode = " + myArray[4]+ newline;
message += " Country = " + myArray[5]+ newline;
location = message;
}

<form name="formDetails" method="" action="">
<br> <a href="#" onclick="getDetails();return false;">send it form onclick</a><br>

whammy...Thanks for the offer, but I won't be publishing this live, immediately, it's a college thing, but when I finish the course I might give you a call when I upload my CV.:thumbsup:

whammy
Dec 7th, 2002, 10:39 PM
Cool that you solved that problem! But make sure your message/form doesn't get too long - or you'll run into another well-known bug with Outlook Express. :)