通过电子邮件发送表单详细信息的最佳方式是什

时间:2014-02-08 01:53:42

标签: javascript php html forms email

新手寻求建议,如果有人可以帮助我,我将不胜感激!

所以我的网站上有一个简单的表格:

<form id="quote" method="post" class="contact" action="mail.php">
                <fieldset>
                    <div id="form_name">
                        <label for="name" class="fixedwidth"> Please enter your full name:</label>
                        <input id="name" name="name" type="text" required aria-required="true" autofocus>
                    </div>

                    <div id="form_mail">
                        <label for="email" class="fixedwidth"> Please enter your email address:</label>
                        <input id="email" name="email" type="email" required aria-required="true">
                    </div>

                    <div id="form_tel">
                        <label for="tel" class="fixedwidth">Please enter you phone number:</label>
                        <input id="tel" name="tel" type="tel" required aria-required="true" >
                    </div>                      

                    <div id="form_message">
                        <label for="message" class="fixedwidth"> Leave a message:</label>
                        <textarea id="message" name="message" cols="20"rows="7"></textarea>
                    </div>

                    <div id="form_submit">
                    <input type="submit" value="Submit">
                    </div>

我想将详细信息发送给自己。我正在使用mail.php:

<?php

$myemail = "myemail@gmail.com";

$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$subject = 'Website query';


$body = "Name: $name E-mail: $email Telephone Number: $tel Subject: $subject Message: $message";

mail($myemail, $subject, $body);
?>

当用户点击“提交”按钮时,我希望感谢您的回复显示在表单和要清除的表单下方。

任何人都可以帮我解决这个问题吗?

万分感谢!

1 个答案:

答案 0 :(得分:1)

您实际上不需要使用javascript来执行此操作,只需将所有内容放在同一页面上并检查POST值是否已发送。

    <form id="quote" method="post" class="contact" action="">
                    <fieldset>
                        <div id="form_name">
                            <label for="name" class="fixedwidth"> Please enter your full name:</label>
                            <input id="name" name="name" type="text" required aria-required="true" autofocus>
                        </div>

                        <div id="form_mail">
                            <label for="email" class="fixedwidth"> Please enter your email address:</label>
                            <input id="email" name="email" type="email" required aria-required="true">
                        </div>

                        <div id="form_tel">
                            <label for="tel" class="fixedwidth">Please enter you phone number:</label>
                            <input id="tel" name="tel" type="tel" required aria-required="true" >
                        </div>                      

                        <div id="form_message">
                            <label for="message" class="fixedwidth"> Leave a message:</label>
                            <textarea id="message" name="message" cols="20"rows="7"></textarea>
                        </div>

                        <div id="form_submit">
                        <input type="submit" value="Submit">
                        </div>
     </fieldset>
</form>
<?php if(isset($_POST['email'])) { // & other checks

   $myemail = "myemail@gmail.com";

   $name = $_POST['name'];
   $email = $_POST['email'];
   $tel = $_POST['tel'];
   $message = $_POST['message'];
   $subject = 'Website query';


   $body = "Name: $name E-mail: $email Telephone Number: $tel Subject: $subject Message: $message";

   if(mail($myemail, $subject, $body)){
     echo "MAIL SENT";
   }
} ?>

只需在输入中添加value =“”即可在重新加载时清除它们。 另一方面,如果您根本不想重新加载页面,则需要使用AJAX javascript

注意:我没有检查你的代码是否有错误我只是给你逻辑但是乍一看似乎没问题。