On form submit, mailto from javascript with form values

泪湿孤枕 提交于 2019-12-11 03:27:52

问题


I have a form, and when the form is submitted (input type="submit"), i would like to open the clients default mail-browser with a pre-populated email-message.

So when the user clicks submit two things need to happen. Open email and submit form.

Also, how can i use the values entered in the form to prepopulate the email?

I'm new to javascript-jquery so please, any code example would be of great help!

Thanks for your help!


回答1:


Before submitting the form you could do:

 window.location.href = 'mailto:nicola@mio.it';

this will open the predefined mail client and you can also prefill some field. look at the mailto sintax here or post some more info so that we can help you;

This could be done like this :

$('input[type=submit]').click(function(){
     window.location.href = "mailto:" + $('#email').val();
});



回答2:


you can prepopulate form with

$("textarea").val('Your message here! You\' have to strip \'');

if it's default email clien't, I'm afraid it's not possible




回答3:


I have used a code like this when I needed to send via mailto using my local email client, it may help:

<html>
<head>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form id="myform" enctype="text/plain" action="test.php" method="post" >
	<input type="text" value="value1" id ="field1" name="field1">
	<input type="checkbox" value="valuel2" id ="field2" name="field2" checked>
	<input type="checkbox" value="value3" id ="field3" name="field3" >
	<textarea id="myText" name ="texty">
	    Lorem ipsum...
	</textarea>
	<button onclick="sendMail(); return false">Send</button>
</form>
<script>
function sendMail() {
	$myform = $('#myform');
	$myform.prop ('action','mailto:mymail@mydomain.es');
	$myform.submit();
}
</script>

</body>
</html>


来源:https://stackoverflow.com/questions/6882355/on-form-submit-mailto-from-javascript-with-form-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!