Click to See Complete Forum and Search --> : Is it possible to submit forms via email depending on user input?
MattOz
05-08-2003, 11:31 AM
Hi there,
I'm really stumped on this and am hoping someone can offer a suggestion...
I need to set up a form whereby a user specifies a number of fields on the page before submitting the page via email. The email address that this information is sent to depends on the selection of one of the mandatory fields on the page. Basically, a user selects a person from a drop down list, and the email is sent to that person.
I have included some of the code here. At the moment, the info is submitted to test@test.com....however, I need this field to be set via the select box included in the code below.
Does anyone have any ideas??
Thanks heaps.
Matt
...
<form name="form1" method="post" action="mailto:test@test.com?subject=Leave request" enctype="text/plain">
<table width="100%" border="0">
<tr>
<td width="1%"> </td>
<td width="33%">Name:</td>
<td width="66%">
<input type="text" name="Name" maxlength="40" size="40"></td>
</tr>
<tr>
<td width="1%"> </td>
<td width="33%">Line manager:</td>
<td width="66%">
<select name="Line manager" size="1">
<option value=""> </option>
<option value="X">Person 1</option>
<option value="Y">Person 2</option>
<option value="Z">Person 3</option>
</select>
</td>
</tr>
<tr>
<td width="1%"> </td>
<td width="33%">
<input type="submit" name="Submit" value="Submit request" onClick="MM_validateForm()"> return MM_returnValue">
</td>
<td width="66%">
<p> </p>
</td>
</tr>
</table>
</form>
...
AdamGundry
05-08-2003, 11:39 AM
This might work?
<form name="form1" method="post" action="mailto:test@test.com?subject=Leave request" enctype="text/plain" onsubmit="setupmailto()">
<script type="text/javascript">
function setupmailto(){
douments.forms[0].action = "mailto:" + document.getElementById('linemanager').value + "?subject=Leave request";
return true;
}
</script>
You will need to add id="linemanager" to your <select> tag.
Adam
MattOz
05-08-2003, 12:17 PM
Love your work Adam! Thanks for your help - it's led me on the right path.
I can now specify the address to send the email to, however, I am only able to send the email to one address. The submit ignores the first action and uses the address set by the select box. I am now using the mailto code:
mailto:test@test.com?cc=recipient2@test.com
to specify two addresses. The problem with this is that I can't specify a subject in my email now.
It's a move in the right direction though. Thanks!!
fakeName
05-08-2003, 12:25 PM
You could do this, but, it only seems to works in IE 5+ and Netscape 7. Not sure why I can't make it work in Opera and Netscape 4.x. Maybe Adam can help with this. One major drawback to sending form data this way to an email program is that you have to rely on the user's settings being config'd to accept this.
<html>
<head>
<title></title>
<script language="JavaScript">
<!--
function writeEmail()
{ var form = document.form1
var selectList = document.form1.LineManager
var selected = selectList.options[selectList.options.selectedIndex].value
// use switch to test which value has been selected from the list
switch (selected) {
case "X" :
form.action = "mailto:Person1@whatever.com?subject=Leave request to Person 1"
break;
case "Y" :
form.action = "mailto:Person2@whatever.com?subject=Leave request to Person 2"
break;
case "Z" :
form.action = "mailto:Person3@whatever.com?subject=Leave request to Person 3"
break;
}
}
</script>
</head>
<body>
<form name="form1" method="post" action="" enctype="text/plain" onSubmit="writeEmail();">
<table width="100%" border="0">
<tr>
<td width="1%"> </td>
<td width="33%">Name:</td>
<td width="66%">
<input type="text" name="Name" maxlength="40" size="40"></td>
</tr>
<tr>
<td width="1%"> </td>
<td width="33%">Line manager:</td>
<td width="66%">
<select name="LineManager" size="1">
<option value="">Directory</option>
<option value="X">Person 1</option>
<option value="Y">Person 2</option>
<option value="Z">Person 3</option>
</select>
</td>
</tr>
<tr>
<td width="1%"> </td>
<td width="33%">
<input type="submit" name="Submit" value="Submit request">
</td>
<td width="66%">
<p> </p>
</td>
</tr>
</table>
</form>
</body>
</html>
AdamGundry
05-08-2003, 01:44 PM
You can add a subject to the mailto: protocol you were using like this:
mailto:test@test.com?cc=recipient2@test.com&subject=Subject
Adam
MattOz
05-09-2003, 05:03 AM
:cool:
Thanks so much for your help guys. I really appreciate it. I can now do what I'd been trying all day yesterday to figure out.
fakename, I was also concerned about using the mailto: protocol to send form data to an email program, but wasn't sure how else to get around it, using HTML/Javascript only. Is there any alternative? Previously, I have sent form data via Java context objects, but I don't have that option here.
Thanks again.
Matt
AdamGundry
05-09-2003, 09:02 AM
You need a server-side language such as PHP if you want to email form data without using the mailto: protocol and the user's email program.
Find out if you have any server-side languages available, and if so search for a form-to-mail script in the appropriate language.
Adam
fakeName
05-09-2003, 09:50 AM
MattOz.
Go to Matt's script archive:
http://www.scriptarchive.com/formmail.html
and download a free copy of FormMail.pl, a server-side cgi form-to-mail processor. Then you don't have to rely on email client and browser settings as previously mentioned.
About half way down that page you'll see FormMail.pl, and an accompanying ReadMe file that shows you how to:
1) Configure the form
2) Configure FormMail
You may be able to pass the email addresses from the select list values. I'm not sure. Usually when configing the script, the emails that you want the data sent to are in a comma delimited list of emails in a hidden form field. So they are predetermined, not user determined.
Usually, when using FormMail, to set the email(s) you want to send the form data to, one would do this:
<input type="hidden" name="recipient" value="someone@somewhere.com"> (you can also have more than one recipient separated by commas).
You could try:
<select>
<option name="recipient" value="person1@somewhere.com"></option>
<option name="recipient" value="person2@somewhere.com"></option>
</select>
Not sure if FormMail will accept this, but, it is worth a try.
If it doesn't work, post to the CGI forum. If you have any prob's configuring FormMail, I'm fairly familiar with configuring it/using it, but I couldn't help at all if you need to decipher the CGI.
MattOz
05-12-2003, 08:44 AM
Thanks for the advice.
I'll see how things go with the mailto for now and may take it from there.
Matt