1

我需要让这个表格像联系表格一样给我发一封电子邮件:

脚本代码:

<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 代码:

   <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>

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

4

2 回答 2

2

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

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

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

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

 <button>SUBMIT</button>

</form>

PHP

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

$to = "someone@example.com";
$subject = "Hello World";
$message = "Firstname: $firstname \n\n Lastname: $lastname";
$from = "sender@example.com";
$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.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

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

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

于 2013-08-11T13:00:43.367 回答
0

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

$to = 'you@domain.com';
$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

于 2013-08-11T13:00:35.210 回答