简体   繁体   English

通过电子邮件发送html表单并将其同时提交到服务器

[英]Email an html form and submit it to the server at the same time

this is probably rather simple, but I didn't really find anything online yet. 这可能很简单,但是我还没有真正在网上找到任何东西。

What I want to do is make a html form with the action="MAILTO:..." tag, so that the user can send a mail with the data filled in. I know how to do this. 我想做的是用action =“ MAILTO:...”标签制作一个html表单,以便用户可以发送带有已填充数据的邮件。我知道该怎么做。 What I also want is that the same data simultanously goes to a php script that stores it in a database. 我还想要的是将相同的数据同时发送到将其存储在数据库中的php脚本。 I also know how to do that (the script part, that is). 我也知道该怎么做(脚本部分)。

What I don't know is how to do both if the user clicks the submit button. 如果用户单击“提交”按钮,我不知道该怎么做。 So, user clicks, gets a mail that he/she can edit and then send, and at the same time, the form data goes to a script that stores the data in a database. 因此,用户单击并获得他/她可以编辑然后发送的邮件,与此同时,表单数据将转到将数据存储在数据库中的脚本中。 They basically can fill in what they want, or leave fields blank, so I don't need to do any checking on content (except for safety reasons, of course). 他们基本上可以填写自己想要的内容,或将字段留空,因此我不需要对内容进行任何检查(当然,出于安全原因除外)。

I'd appreciate any pointers. 我将不胜感激任何指针。

Thanks. 谢谢。

You can do it by using javascript and ajax like this (with jquery): 您可以使用javascript和ajax这样(使用jquery)来做到这一点:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
        function sendData(){
               var request = $.ajax({
                  url: "yourApp.php",
                  data: $("#myform").serialize(),
                  type: 'POST'
               });

            request.done(function(){
                  $("#myform").submit();
             });
        }
        </script>
    </head>
    <body>
        <form id="myform" action="MAILTO:yourDestination@mail.com" method="post" enctype="text/plain">
            Name:<br>
            <input type="text" name="name" value="your name"><br>
            E-mail:<br>
            <input type="text" name="mail" value="your email"><br>
            Comment:<br>
            <input type="text" name="comment" value="your comment" size="50"><br><br>
            <input type="button" value="Send" onclick="sendData()">
        </form>
    </body>
</html>

This way you will send your data to your application and then email it 这样,您会将数据发送到应用程序,然后通过电子邮件发送

From what i understand you want both actions to happen upon submit, the problem is that using the MAILTO action in a form is on the client side and posting the data to PHP script is on the server side and you cant have 2 actions just in HTML (assuming that you want the mail to be opened and displayed to the user). 据我了解,您希望两个动作都在提交时发生,问题是在表单中使用MAILTO动作是在客户端,将数据发布到PHP脚本是在服务器端,而您不能仅在HTML中有2个动作(假设您希望打开邮件并将其显示给用户)。 In order to do that i would suggest using JS, keep the action of the form as MAILTO, add an event for the form submission, and before you submit the form - which will open the email - run an AJAX call to post the data. 为了做到这一点,我建议使用JS,将表单的动作保留为MAILTO,为表单提交添加一个事件,然后在提交表单(这将打开电子邮件)之前,运行AJAX调用以发布数据。

You can also have the form with one action and upon submission just create a 'fake' new form with same data but different action attribute and submit it. 您也可以使用一个动作来处理表单,提交后只需创建一个具有相同数据但动作属性不同的“假”新表单,然后提交即可。 Just remember that submitting a form to a page will reload the page. 只要记住,向页面提交表单将重新加载页面。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM