2014-02-08 69 views
0

新手尋求建議,將不勝感激,如果有人可以幫助我!電子郵件表單詳細信息的最佳方式是什麼?

所以,我有我的網站上簡單的形式:

<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 = "[email protected]"; 

$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

你實際上並不需要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 = "[email protected]"; 

    $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 =「」以在重新加載時清除它們。 另一方面,如果你不希望頁面重新加載,你將需要JavaScript與AJAX

注意:我沒有檢查你的代碼的錯誤我只是給你的邏輯,但乍一看它似乎沒問題。

+0

感謝您的回覆。頁面無法重新加載,所以我想我必須深入研究Javascript和AJAX。 –

相關問題