0

I'm working on a project that's on a small scale for now. It's more about presentation and less about security.

All I need to do is have a form that will be filled out, then the user clicks submit and all the information collected from the form will be sent to a specified 'mailto:address'. I know that it would make a lot more sense to do this using php, but for now I only have html/css to work with.

Is there a way I can submit everything on the form to an email address? It can open up the user's email handler, but it doesn't have to.

I have 11 text fields and 2 sets of radios.

baao
  • 71,625
  • 17
  • 143
  • 203
Sani Evans
  • 81
  • 1
  • 1
  • 11
  • How it is possible using only HTML/CSS to email submission.? you can use ajax to submit and email form. – Zeeshan Dec 09 '14 at 05:55
  • I'm not sure, I'm still learning a lot. I'll look into it, but I can't use server side programming because I don't have a server to work with. I'm extremely limited on what languages I have at my disposal. I can use html css and JavaScript, basically anything front end, but I can't use any back end sever side languages – Sani Evans Dec 09 '14 at 05:58
  • 1
    If u use jquery u can do this easy. Create a link with href #. Bind a click event to this anchor, In this event serialize the form and Change the href to mailto with in tbe querystring body=`the serialized data` – DarkBee Dec 09 '14 at 06:03
  • There are olso 'online' form collection services' that you can use e.g. [Wufoo is a web application that helps anybody build amazing online forms. It collects the data as well](http://www.wufoo.com/). There are many others. – Ryan Vincent Dec 09 '14 at 22:19

5 Answers5

2

Well, what you are asking is totally possible, and I think the selected right answer is wrong. Take a look and read these two links:

Customize mailto links http://blog.escapecreative.com/customizing-mailto-links/

The default action of mailto depends on the browser so, if you try it and my solution could not work in your browser the cause may be for this reason: Configuring mailto Mailto links do nothing in Chrome but work in Firefox?

Here is the jsfiddle: http://jsfiddle.net/g3qazhf8/ and I'm using jquery to make my life easier:

javascript

function sendEmail(){
   var answerArr = [];
   $("input, textarea").each(function(){       
       if($(this).attr("type") === "radio"){            
           if($(this).is(":checked")){
              answerArr.push($(this).val());
           }
       }else{
          answerArr.push($(this).val());       
       }
   });

    var body = answerArr.join("\n");
    var mailto = "whateveremail@gmail.com";

    var a = document.createElement('a');
    a.href = "mailto:" + mailto + "?body=" + escape(body);
    console.log(a.href);
    a.click();
}

$(document).on("click",".submit",sendEmail);

HTML

<textarea> your message </textarea>
<input type="text" placeholder="name" name="name" />
<input type="text" placeholder="lastname" name="lastname" />
<label>
<input type="radio" name="rboption" value="yes" /> YES
</label>
<label>
<input type="radio" name="rboption" value="no" />NO
</label>
<br>
<a href="#" target="_blank" style="display:inline-block; background:#333; color:#FFF; padding:5px 10px 5px 10px; margin:15px 0 0 0; text-decoration:none" class="submit">Sent the email</a>

Btw, you should try the solution on your localhost. I will attach an image proving that it's working: enter image description here enter image description here

Community
  • 1
  • 1
ncubica
  • 8,169
  • 9
  • 54
  • 72
  • I agree that the selected answer is incorrect. I think your answer should be the official one because you prove the feasibility of using form data with front-end technologies, using jquery advantageously coupled with a clever use of DOM. – slevy1 Dec 09 '14 at 23:48
  • 1
    It worked, thank you very much! I appreciate you taking the time to help me out on this. Unfortunately, I had already submitted my template to the people I was working for. They have a team of programmers who could use server side scripts to get the job done, I was just hoping for the sake of presentation and a test run that I could've done it with javascript just so they had more to see upon its initial presentation. Too late on my part, unfortunately. But again- thank you very much! This community is so helpful – Sani Evans Dec 16 '14 at 07:08
0

You can't do this using only html and css. Both languages are used and made for layouting purposes, but not for data processing. To send an email from your program, you will have to use a processing language like php. (there are several other options to php, but html is none)

Even though you can link to an email address

<a href="mailto:awesome@mail.com">...</a>

you won't be able to process and display your form's data within the opening mail dialog.

baao
  • 71,625
  • 17
  • 143
  • 203
  • Alright, thank you! I'll let the people I'm working with know it can't be done with what I'm limited to and see what they have to say. I appreciate the answer – Sani Evans Dec 09 '14 at 06:13
  • There are two answers below that prove that one may use mailto with form data using front-end technologies, as long as one uses JavaScript or jQuery. – slevy1 Dec 10 '14 at 00:14
  • @SharonLevy true, but the question is about html and css – baao Dec 10 '14 at 00:21
  • 1
    The developer asking the question doesn't realize that there is more at his disposal than merely HTML and CSS; there's JavaScript, too. Note the actual question: "Is there a way I can submit everything on the form to an email address?" Two people have responded in the affirmative "Yes". I demo this fact with JavaScript and ncubica uses the JavaScript library jQuery. You may not like these truths but they are nonetheless true. – slevy1 Dec 10 '14 at 00:30
0

As it turns out submitting the form is unnecessary. Create an HTML form using the kinds of form elements that you wish. Here's what my form looks like:

<form>
to:<input name="mailto" type="text">
re:<input name="subject" type="text">
body:<textarea name="body"></textarea>
<input type="button" onclick="processForm()">
</form>

Note, the last input is a button which prevents submission of the form. When pressed, the element's onclick event attribute causes processForm() to execute. The function accesses the form values and serializes all except for the button, tweaking the resulting string so that it is usable for the mailto protocol, as follows:

<script>
function processForm(){
  var inputs  = document.getElementsByTagName("input");
  var message = document.getElementsByTagName("textarea");

  var str = inputs[0].name + ":" + inputs[0].value;     // mailto:[email address]
  str += "?" + inputs[1].name + "=" + inputs[1].value;  // ?subject=[subject]

  str += "&" + message[0].name + "=" + message[0].value;// &body=[body]
  str += str.replace(/\+/ig,"%20");                     // url encode space char '+'

  location.href=str;                                    // invoke mailto

}
</script>

The final adjustment to str is for it to be urlencoded which is the reason for replacing '+' with '%20'. If other content needs encoding, then I would use the function encodeURI(), passing str as a parameter and assign the result to a variable.

The last line of the script invokes the mailto protocol which causes whatever emailer is on your system to activate. On my system, Outlook springs up with all the desired fields automatically filled in. I tested the preceding code by sending an email to myself and it worked.

You could simplify serializing the form data with "JavaScript Form Serialize". Note: you'll still need to tweak the resulting string. See below:

<script type="text/javascript" src="js/serialize-0.2.js"></script>
<script type="text/javascript">
document.getElementById("Serialize").onclick = function () {
    var str = serialize(document.forms[0]);
    var arr = str.split("&");
    arr.reverse();
    str = arr.join("&");
    str = str.replace("mailto=","mailto:");
    str = str.replace("&subject", "?subject");
    location.href = str;
};
</script>

With this solution, I replaced the button with a link. When the form data is passed to serialize(), the resulting output has the data in reverse order with the body appearing first. arr.reverse() restores the correct order. The remaining adjustments are so that str works with mailto.

For more info about mailto there is a detailed technical explanation here. The following link is beneficial if you just want to know the basics.

slevy1
  • 3,797
  • 2
  • 27
  • 33
0

Just use elFormo.com and let it do the form processing and send you an email. All you have to do is create a form in HTML and post it to the elFormo app. They even have a free plan, and the paid plans are cheap (under $5 month).

JDStraughan
  • 317
  • 4
  • 12
-2

You can post form data using just HTML, CSS and JQuery. I would try using an alternative such as Google Forms or another third party software.