Over a million developers have joined DZone.

What If Your Contact Form Fails?

DZone 's Guide to

What If Your Contact Form Fails?

· Web Dev Zone ·
Free Resource

Did you ever think about it? Someone types the message, clicks on send and gets the error message like "Something went wrong. Sorry...". Ok, what now? How TF can I send you a message?

Contact form is often the only way you can communicate with someone. People just don't like to expose their email addresses just like that. But having a contact form means that it might fail at some point. Your SMTP server could be down or your application could just stop working. It happened so many times.

So what can you do?

Well, instead of showing seamless error message and informing users that your application broke, you can offer them an alternative. Instead of showing a message "An error occurred. Your email cannot be sent", you can show "An error occurred and your email cannot be sent. However, you can still contact me on my@email.com". Much better, isn't it?

Better? Yes, but not good enough

What if someone just typed several paragraphs? There is a small chance that he/she will type it again unless it is something seriously important. There is very simple trick to avoid this problem. Literally, copy the text user typed and paste it into the email link.

If you have these two input fields (beside other) in your contact form:

<input type="text" id="subject">
<input type="text" id="message">

you can create email link dynamically with this simple JavaScript code:

<script type="text/javascript">
var subject = document.getElementById("subject").value;
var msg = document.getElementById("message").value;
var link = document.getElementById("emailme");

link.href="mailto:my@email.com?subject=" + subject + "&body=" + msg;

And your error message could look like this:

<div id="errorMessage">Error Occured. Please contact me on <a id="emailme" href="my@email.com">my@email.com</a></div>

Pretty simple, but do you know any other way to do this? How would you solve this problem?


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}