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

Adding a variable to mailto

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all

The following code represents the desired result whereby a visitor enters their name into a textbox on a page and the variable created, var, is pasted into the body of the resultant email.

<a href=&quot;mailto:webmaster@btopenworld.com?subject=Subscribe to mailing list&body=My name is (var)%0d%0dI understand that by subscribing to the mailing list, my email address will not be disclosed to any third party%0d%0dPlease add me to your list&quot;>Subscribe to list</a>

Any guidance as to how implement this would be appreciated HTH

Chris [pc2]
 
You might want to change it a little but I think this is what you were looking for:



<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<TITLE></TITLE>
<Script Language=&quot;Javascript&quot;>
<!--
function mailIt(x)
{
// SET MESSAGE VALUES
var to = &quot;webmaster@internet.com&quot;;
var cc = &quot;another_person@company.com&quot;;
var bcc = &quot;yet_another_person@company.com&quot;;
var subject = &quot;Subscribe to mailing list&quot;;
var body = &quot;This is a test for &quot; + x + &quot; about the mailing list.&quot;

alert(x);

// BUILD MAIL MESSAGE COMPONENTS
var doc = &quot;mailto:&quot; + to +
&quot;?cc=&quot; + cc +
&quot;&bcc=&quot; + bcc +
&quot;&subject=&quot; + escape(subject) +
&quot;&body=&quot; + escape(body);

// POP UP EMAIL MESSAGE WINDOW
window.location = doc;
}
//-->
</Script>
</HEAD>
<BODY>

<Form name=&quot;frmTest&quot; method=&quot;POST&quot;>
<INPUT type=&quot;text&quot; id=&quot;txtName&quot; name=&quot;Name&quot;>
<INPUT type=&quot;button&quot; value=&quot;Should submit&quot; onclick=&quot;mailIt(txtName.value);&quot;>
</Form>


<a href=&quot;Javascript:mailIt(document.frmTest.txtName.value);&quot;>Send</a>

</BODY>
</HTML>
 
mithrilhall

Thanks for that.

How would you errortrap an empty value of x, so that the user was required to enter a value before the function mailIt() fired? HTH

Chris [pc2]
 
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<TITLE></TITLE>
<Script Language=&quot;Javascript&quot;>
<!--
function mailIt(x)
{
if (x == ''||x == null)
{
alert('Please put in your name');
}
else
{
// SET MESSAGE VALUES
var to = &quot;webmaster@internet.com&quot;;
var cc = &quot;another_person@company.com&quot;;
var bcc = &quot;yet_another_person@company.com&quot;;
var subject = &quot;Subscribe to mailing list&quot;;
var body = &quot;This is a test for &quot; + x + &quot; about the mailing list.&quot;

// BUILD MAIL MESSAGE COMPONENTS
var doc = &quot;mailto:&quot; + to +
&quot;?cc=&quot; + cc +
&quot;&bcc=&quot; + bcc +
&quot;&subject=&quot; + escape(subject) +
&quot;&body=&quot; + escape(body);

// POP UP EMAIL MESSAGE WINDOW
window.location = doc;
}
}
//-->
</Script>
</HEAD>
<BODY>

<Form name=&quot;frmTest&quot; method=&quot;POST&quot;>
<INPUT type=&quot;text&quot; id=&quot;txtName&quot; name=&quot;Name&quot;>
<INPUT type=&quot;button&quot; value=&quot;Should submit&quot; onclick=&quot;mailIt(txtName.value);&quot;>
</Form>


<a href=&quot;Javascript:mailIt(document.frmTest.txtName.value);&quot;>Send</a>

</BODY>
</HTML>





 
mithrilhall

That's fine - how would you substitute the following code for the &quot;alert&quot;?

<script language=&quot;VBScript&quot;>
MsgBox &quot;Please enter your name&quot;,48,&quot;Input Error&quot;
</script>

TIA
HTH

Chris [pc2]
 
<Script Language=&quot;Javascript&quot;>
<!--
function mailIt(x)
{
x = prompt('Please put in your name!','');

// SET MESSAGE VALUES
var to = &quot;webmaster@internet.com&quot;;
var cc = &quot;another_person@company.com&quot;;
var bcc = &quot;yet_another_person@company.com&quot;;
var subject = &quot;Subscribe to mailing list&quot;;
var body = &quot;This is a test for &quot; + x + &quot; about the mailing list.&quot;

// BUILD MAIL MESSAGE COMPONENTS
var doc = &quot;mailto:&quot; + to +
&quot;?cc=&quot; + cc +
&quot;&bcc=&quot; + bcc +
&quot;&subject=&quot; + escape(subject) +
&quot;&body=&quot; + escape(body);

// POP UP EMAIL MESSAGE WINDOW
window.location = doc;

}
//-->
</Script>
 
mithrilhall

Unfortunately that's not what I was after.

I was curious as to how you mix javascript and vbscript together within the same javascript function, assuming it's even possible.

<script language=&quot;VBScript&quot;>
MsgBox &quot;Please put in your name&quot;,48,&quot;Input Error&quot;
</script>

is the equivalent, plus title of &quot;Input Error&quot;, of

alert('Please put in your name'); HTH

Chris [pc2]
 
VBScript only runs on IE I think. Do you just want this to work on IE? Also, can you use a server-side script?
 
mithrilhall

I take your point about IE only if using vbscript, and as yet I don't have an answer about a server-side script - still looking at ISPs.

alert with the browser name as the title lacks visual refinement - vbscipt is better, but not a whole lot.

A modal popup with an 'OK' button that would close the window would probably be the best answer. HTH

Chris [pc2]
 
mithrilhall

This is a reworked version of your original function, 'if' section only, with a self closing window to replace 'alert'.

function mailIt(x)
{
if (x == ''||x == null)
{
config='toolbar=no,location=no,directories=no,status=no,menubar=no,width=210,height=120'

config += 'scrollbars=no,resizable=no'
pop = window.open (&quot;&quot;,&quot;pop&quot;,config)

pop.document.write('<script language=&quot;javascript&quot;>');
pop.document.write('setTimeout(');
pop.document.write('&quot;self.close()');
pop.document.write(';&quot;,3000)');
pop.document.write('</');
pop.document.write('script>');
pop.document.write('<body bgcolor=#cdf9fc>');
pop.document.write('<center><font = Verdana><b><h2>Error</h2></b></font></center>');
pop.document.write('<center><font = Verdana><h5>You did not enter your name</h5></font></center>');
pop.document.write('<center><font = Verdana><h5>Please try again</h5></font></center>');
pop.document.write('</body>');
}

How could you either place 'Error' in the title bar and dispense with the URL, or remove the title bar altogether? HTH

Chris [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top