Php 如何创建联系人表单并将其发送到您的电子邮件?

Php 如何创建联系人表单并将其发送到您的电子邮件?,php,forms,contact-form,Php,Forms,Contact Form,我目前正在做一个项目;但是,我需要一个联系表单,让用户提交他们的信息并将信息发送到电子邮件。我喜欢HTML和CSS,但不喜欢php。我正在网上学习一个教程,很难理解。有人能帮忙吗 谢谢 另外,如果它被发送到我的谢谢.html页面,我会很高兴的。与中一样,当用户提交表单时,它会将用户重定向到my Thanke.html页面 HTML: 让我们知道一些事情! php: 我已经修好了!这是代码,以防任何人看到这一点,并希望完成像我这样的联系形式 谢谢 HTML: 让我们知道一些事情! php:

我目前正在做一个项目;但是,我需要一个联系表单,让用户提交他们的信息并将信息发送到电子邮件。我喜欢HTML和CSS,但不喜欢php。我正在网上学习一个教程,很难理解。有人能帮忙吗

谢谢

另外,如果它被发送到我的
谢谢.html
页面,我会很高兴的。与中一样,当用户提交表单时,它会将用户重定向到my Thanke.html页面

HTML:

让我们知道一些事情!
php:


我已经修好了!这是代码,以防任何人看到这一点,并希望完成像我这样的联系形式

谢谢

HTML:

让我们知道一些事情!
php:



您面临的问题是什么?我的联系人表单在提交时不起作用。我需要它将提交的信息发送到电子邮件。我想这也会重定向到我的Thanke.html页面<代码>标题('Location:Thanke.html')首先修复表单。您需要将(示例)name=“input fname”添加到相应的输入中。还要将method=“POST”添加到表单标记中
  <h1 id="contact-us">Let us know something!</h1>
    <form action="contact.php" class="cf">
      <div class="half left cf">
         <input type="text" id="input-fname" placeholder="First Name">
         <input type="text" id="input-lname" placeholder="Last Name">
         <input type="email" id="input-email" placeholder="Email Address">
      </div>
      <div class="half right cf">
        <textarea name="message" type="text" id="input-message" placeholder="Message"></textarea>
      </div>
      <input type="submit" value="Submit" id="input-submit">
   </form>
<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "asdf@myemail.com";
    $email_subject = "my subject";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['input-fname']) ||
        !isset($_POST['input-lname']) ||
        !isset($_POST['input-email']) ||
        !isset($_POST['input-message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }



    $first_name = $_POST['input-fname']; // required
    $last_name = $_POST['input-lname']; // required
    $email_from = $_POST['input-email']; // required
    $comments = $_POST['input-message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>



<?php

}
?>
<h1 id="contact-us">Let us know something!</h1>
     <form action="contact.php" method='post' class="cf">
       <div class="half left cf">
          <input type="text" id="fname" name="fname" placeholder="First Name">
          <input type="text" id="lname" name="lname" placeholder="Last Name">
          <input type="email" id="email" name="email" placeholder="Email Address">
       </div>
       <div class="half right cf">
         <textarea name="message" type="text" id="message" placeholder="Message"></textarea>
       </div>
       <input type="submit" value="Submit" id="input-submit" name='send'>
    </form>
<?php
if(isset($_POST['send'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "hunter.shaw@cei.edu";
    $email_subject = "D-Tail Doctor Form Info";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['fname']) ||
        !isset($_POST['lname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }



    $first_name = $_POST['fname']; // required
    $last_name = $_POST['lname']; // required
    $email_from = $_POST['email']; // required
    $comments = $_POST['message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
}

// redirects them to link below
header('Location: thanks.html');

?>