2017-04-20 158 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 
    } 
?>