使用电子邮件操作提交 Javascript 表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18172018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:57:11  来源:igfitidea点击:

Javascript Form Submit with Email Action?

phpjavascripthtml

提问by Isleno Ituriel

I need make this form send me a email like a contact form:

我需要让这个表格给我发一封电子邮件,比如联系表格:

Script code:

脚本代码:

<script type="text/javascript">
        $(document).ready(function(){

            $("#contactLink").click(function(){
                if ($("#contactForm").is(":hidden")){
                    $("#contactForm").slideDown("slow");
                }
                else{
                    $("#contactForm").slideUp("slow");
                }
            });

        });

        function closeForm(){
            $("#messageSent").show("slow");
            setTimeout('$("#messageSent").hide();$("#contactForm").slideUp("slow")', 2000);
       }
       </script>

HTML CODE:

HTML代码:

   <div class="box">
        <div id="contactFormContainer">
            <div id="contactForm">
                <fieldset>
                    <label for="Name">Nome: </label>
                    <input id="name" type="text" />
                    <label for="Telefone">Telefone Fixo: </label>
                    <input type="text" id="phone" maxlength="15" onkeypress="Mascara(this);" />
                    <label for="Message">Assunto:</label>
                    <textarea id="Message" rows="3" cols="20"></textarea>
                    <input id="sendMail" type="submit" name="submit" onclick="closeForm()" />           
                    <span id="messageSent">Sua solicita??o foi enviada com sucesso, por favor, aguarde...</span>
               </fieldset>
            </div>
            <div id="contactLink"></div>
        </div>

When click and close the form i need send me a email with the content of form, how to? Some idea? thanks!

当单击并关闭表单时,我需要向我发送一封包含表单内容的电子邮件,如何?有什么想法?谢谢!

回答by Dalton Pereira

Firstly i can't see the form tags in your code. According to me you're doing this wrong and i'm sure many of our friends on stack will agree too.

首先,我在您的代码中看不到表单标签。根据我的说法,你做错了,我相信我们的许多堆栈朋友也会同意。

Your question suggests that you basically want to receive an email with the data submitted through the form. Why don't you try the below method.

您的问题表明您基本上希望收到一封包含通过表单提交的数据的电子邮件。你为什么不试试下面的方法。

HTML

HTML

<form action="mail.php" method="POST">

 <input type="text" name="fname"></input>

 <input type="text" name="lname"></input>

 <button>SUBMIT</button>

</form>

PHP

PHP

<?php
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];

$to = "[email protected]";
$subject = "Hello World";
$message = "Firstname: $firstname \n\n Lastname: $lastname";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

The above example is the most simplest method of sending an email. You can go advance by adding more header information and graphically formatting the email.

上面的例子是最简单的发送电子邮件的方法。您可以通过添加更多标题信息和以图形方式格式化电子邮件来继续前进。

Go through these tutorials if you get confused.

如果您感到困惑,请阅读这些教程。

http://www.w3schools.com/php/php_mail.asp

http://www.w3schools.com/php/php_mail.asp

http://www.phpeasystep.com/phptu/8.html

http://www.phpeasystep.com/phptu/8.html

And since you mentioned that you want to perform the task via javascript you can try submitting the form via ajax, refer the below tutorials

既然您提到要通过 javascript 执行任务,您可以尝试通过 ajax 提交表单,请参阅以下教程

http://teachingyou.net/php/simple-php-contact-form-using-ajax/

http://teachingyou.net/php/simple-php-contact-form-using-ajax/

http://www.sitepoint.com/forums/showthread.php?1055068-Send-PHP-email-using-jQuery-AJAX

http://www.sitepoint.com/forums/showthread.php?1055068-Send-PHP-email-using-jQuery-AJAX

回答by ChristynCanada

Since you've tagged the question php, have a look at php's mail function. http://php.net/manual/en/function.mail.php

既然你已经标记了问题 php,那么看看 php 的邮件功能。http://php.net/manual/en/function.mail.php

$to = '[email protected]';
$subject = 'Contact Form';
$message = '...' //concatenate the $_POST (or $_GET) variables to create this message

mail($to, $subject, wordwrap($message, 70, "\r\n");

This function requires that your server has a properly configured to send mail - see the php documentation for requirements: http://www.php.net/manual/en/mail.requirements.php

此功能要求您的服务器已正确配置以发送邮件 - 有关要求,请参阅 php 文档:http: //www.php.net/manual/en/mail.requirements.php