I just finished a quick project where the client wanted a HTML form that he could host locally on his PC. The form was to be filled out often, and upon submitting, the contents are  emailed to the desired representative in the field. Sound easy? Leaving a blank mailto won’t work.

To create a form like this start out by building your regular web page, and whatever form fields that you desire. To get the mailto action to work for various emails there is only one trick, and all it requires is some javascript.

Below is the javascript and HTML for the form fields you will need to edit:

<body>
<SCRIPT language=”JavaScript”>
function OnSubmitForm()
{
if(document.pressed == ‘Email‘)
{
var email = prompt(“Please enter the email the form will be submitted to:”);
var toemail = “mailto:’” + email + “‘”;
document.emailform.action = (toemail);
}
}
</SCRIPT>
<form name=”emailform” onSubmit=”return OnSubmitForm();” method=”post” enctype=”text/plain”>
<Misc Text Fields>
<input type=”submit” onClick=”document.pressed=this.value” VALUE=”Email” />
</body>

Starting with the form, the only thing unusual is the onSubmit attribute. It’s telling the form to complete the javascript function once the submit button has been clicked. Notice the submit button also has a little javascript.

If you are having a problem getting this to work, look how I color coded. You may just be missing or not identifying elements correctly.

  220 Responses to “Variable Email Form with Mailto”

  1. Easy and useful javascript, just need to copy and add the eamil address :)

  2. The the power of javascript can do… cool..

  3. That’s the power of javascript… cool..

  4. Good JavaScript coding it is. One of the best blog post I read in recent past. This post have great content. I can stand up and say superb! There is sufficient reason why you got more visitors to your article.

  5. Good post, thanks. I was actually wondering how to do this myself.. I always get my cousin to help me with it!

  6. Nice thanks. Its always good to have options.

  7. You are having a knack for simplicity !

  8. Wonderful post, I really enjoyed reading it! The people you meet when you travel really are often the best part of it all!

  9. Fully agree with your points here. All the posts are fully informative and having very good themes. I love to visit your blog again and again its really increase my knowledge. Thanks for the awesome post.

  10. the 3DTV Reviewer Blog has more impact (for me) and it is still very readable.

  11. Hi,

    I’d like to offer a selection, from a drop down list of recipients, to whom the FORM should be emailed.

    When using ‘mailto:’ – is it possible to use a variable to set this?
    If so, can anyone suggest some code to demo this please?

    • I just tried to reply with a block of code, but it looks like it renders it as HTML and it won’t show up, I’ll try to create it on an external page and link you up.

  12. thanks succesfull ? am like1

  13. thanks succesfull ? am like2

  14. thanks succesfull ? am like3

  15. thanks succesfull ? am like4 thanks futurent

  16. I will try this javascript code for my website. thanks for this code.

  17. Very nice java script coding. I appreciate your knowledge.
    Thanks for sharing…

  18. This is a very useful script. Thank you for sharing

  19. This is a great script thank you for sharing it us

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>