2017-04-20 162 views
-1

我正在爲我的網頁編寫聯繫表格,但有些東西不起作用,我無法找到問題所在。當我按提交按鈕它剛剛重新加載網頁。如果有人不介意檢查我的代碼,我將不勝感激。「給我發電子郵件」聯繫表格不起作用

<?php 
if(isset($_POST['submit'])){ 
    $to = "[email protected]"; // this is your Email address 
    $from = $_POST['email']; // this is the sender's Email address 
    $name = $_POST['name']; 
    $subject = "Form submission"; 
    $subject2 = "Copy of your form submission"; 
    $message = $name . " wrote the following:" . "\n\n" . $_POST['message']; 
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message']; 

    $headers = "From:" . $from; 
    $headers2 = "From:" . $to; 
    mail($to, $subject, $message, $headers); 
    // You can also use header('Location: thank_you.php'); to redirect to another page. 
    } 
?> 


<section id="contact" class="parallax-section"> 
    <div class="overlay"></div> 
    <div class="container"> 
     <div class="row"> 

      <div class="col-md-offset-2 col-md-8 col-sm-offset-1 col-sm-10"> 
      <div class="wow fadeInUp section-title" data-wow-delay="0.3s"> 
       <h2>Susisiekite su mumis</h2> 
       <h4>mes visada pasiruoše atsakyti į jums rūpimus klausimus</h4> 
      </div> 
       <div class="contact-form wow fadeInUp" data-wow-delay="0.7s"> 
        <form id="contact-form" method="POST" action="#"> 
         <input name="name" type="text" class="form-control" placeholder="Vardas, Pavardė" required> 
         <input name="email" type="email" class="form-control" placeholder="Elektroninis paštas" required> 
         <textarea name="message" class="form-control" placeholder="Jūsų žinutė" rows="5" cols="30" required></textarea> 
         <input type="submit" class="form-control submit" name="submit" value="SIŲSTI"> 
        </form> 
       </div> 
      </div> 

     </div> 
    </div> 
</section> 
+1

是您的網絡服務器配置爲使用'郵件()'? Afaik沒有開箱即可使用。 – Sirko

+0

不清楚的問題,你能證明這是小提琴嗎? – Sushan

+0

不太清楚,但請使用'error_reporting(E_ALL); ini_set('display_errors',1);'在你的頁面頂部,讓我們知道如果PHP返回任何錯誤 – OldPadawan

回答

0

你的問題有點不清楚。但是,表單會再次顯示,因爲您的代碼已設置爲始終顯示錶單。如果您希望顯示一條消息,指出該電子郵件已發送,則應在您的if塊中放置回聲聲明。如果你不想顯示錶單,你可以把表單放在else塊中。像這樣的東西應該工作:

<?php 
if(isset($_POST['submit'])){ 
    $to = "[email protected]"; // this is your Email address 
    $from = $_POST['email']; // this is the sender's Email address 
    $name = $_POST['name']; 
    $subject = "Form submission"; 
    $subject2 = "Copy of your form submission"; 
    $message = $name . " wrote the following:" . "\n\n" . $_POST['message']; 
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message']; 

    $headers = "From:" . $from; 
    $headers2 = "From:" . $to; 
    $success = mail($to, $subject, $message, $headers); 
    if ($success) { 
     echo "Your message has been sent."; 
    } else { 
     echo "An error was encountered."; 
    } 
} 
else { //begin else section 
?> 


<section id="contact" class="parallax-section"> 
    <div class="overlay"></div> 
    <div class="container"> 
     <div class="row"> 

      <div class="col-md-offset-2 col-md-8 col-sm-offset-1 col-sm-10"> 
      <div class="wow fadeInUp section-title" data-wow-delay="0.3s"> 
       <h2>Susisiekite su mumis</h2> 
       <h4>mes visada pasiruoše atsakyti į jums rūpimus klausimus</h4> 
      </div> 
       <div class="contact-form wow fadeInUp" data-wow-delay="0.7s"> 
        <form id="contact-form" method="POST"> 
         <input name="name" type="text" class="form-control" placeholder="Vardas, Pavardė" required> 
         <input name="email" type="email" class="form-control" placeholder="Elektroninis paštas" required> 
         <textarea name="message" class="form-control" placeholder="Jūsų žinutė" rows="5" cols="30" required></textarea> 
         <input type="submit" class="form-control submit" name="submit" value="SIŲSTI"> 
        </form> 
       </div> 
      </div> 

     </div> 
    </div> 
</section> 

<?php 

} //closes else section 
0

你可以保留的形式action屬性爲空白:

<form id="contact-form" method="POST" action=""> 

和調試你的PHP代碼:

<?php 
    if (isset($_POST['submit'])) { 
     $to = "[email protected]"; // this is your Email address 
     $headers = "From:" . $from; 
     $headers2 = "From:" . $to; 
     $send_email = mail($to, $subject, $message, $headers); 
     echo "Debugging mail send code.... <br/>"; // Add this line 
     var_dump($send_email); // Add this line. Check if it's true or false 
    } 
?>