简体   繁体   中英

What is the cause of this error with simple php contact form?

I have made my website live and decided to test out my contact form, which is a simple php script I found online. It seems simple enough, but I can't figure out why I am getting the die "Error!" when I fill in all the fields and click submit on my live website. I don't have any experience working with php, so any help here would be very appreciated! PHP:

<?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];
  $formcontent="From: $name \n Email: $email \n Subject: $subject \n Message: $message";
  $recipient = "email@gmail.com";
  $subject = "Customer Contact Message";
  $mailheader = "From: $email \r\n";
  mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
  echo "Thank you for contacting us.";
?>

HTML:

<form class="contact-form" id="contact-form" action="contactform.php" method="post">
  <div class="col-md-6">
    <input placeholder="Name" name="name" type="text">
  </div>

  <div class="col-md-6">
    <input placeholder="Email" name="email" type="text">
  </div>

  <div class="col-md-12">
    <input placeholder="Subject" name="subject" type="text">
  </div>
  <div class="col-md-12">
    <textarea placeholder="Message" name="message"></textarea>
  </div>
  <div class="col-md-12">
    <button class="btn submit-btn btn-primary" type="submit">Send Message</button>
  </div>
</form>
<button class="btn submit-btn btn-primary" type="submit">Send 
Message</button>
instead use this:

<input type="submit" class="btn submit-btn btn-primary" value="Send Message" 
name="submit">

And your php script will be like this:
<?php
    if(isset($_POST['submit'])){
       $name = $_POST['name'];
       $email = $_POST['email'];
       $subject = $_POST['subject'];
       $message = $_POST['message'];
       $formcontent="From: $name \n Email: $email \n Subject: $subject \n 
       Message: $message";
       $recipient = "email@gmail.com";
       $subject = "Customer Contact Message";
       $mailheader = "From: $email \r\n";
       mail($recipient, $subject, $formcontent, $mailheader) or 
       die("Error!");
       echo "Thank you for contacting us.";
   }    
?>

当我刚开始学习PHP时,发现设置电子邮件表单很麻烦,我建议您尝试以下服务: https : //formspree.io/,它免费为您简化了整个过程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM