联系表格,它是如何工作的?

Contact Form, how does it work?

我即将启动我的网站,但首先我需要完成我的联系表格,以确保用户可以联系我。我不是 PHP 或任何相关领域的专家,所以请理解我的无知。

我的服务器已准备就绪 运行。我有一封电子邮件。我有网站。我有两个文档,一个 contact.html 和另一个 send.php.

我的contact.html内容如下:

<!doctype html>
<html>
<head>
  <title>Spotnight - About</title>
  <link rel="stylesheet" href="style.css">
  <link rel="Favicon" href="f.ico" type="image/x-icon">
</head>
<body>
  <div class="ham_container">
    <svg id="hamburger" title="Menu">
      <path d="M0,5 30,5" stroke="#eeeeee" stroke-width="5"/>
      <path d="M0,14 30,14" stroke="#eeeeee" stroke-width="5"/>
      <path d="M0,23 30,23" stroke="#eeeeee" stroke-width="5"/>
    </svg>
  </div>
  <nav>
    <ul>
      <li><a href="http://spotnight.io/">Our Home</a></li>
      <li><a href="http://spotnight.io/about.html">About Us</a></li>
      <li><a href="http://spotnight.io/hybon.html">Hybon Sites</a></li>
      <li><a href="http://spotnight.io/orion.html">Orion Interactives</a></li>
      <li><a href="http://spotnight.io/studios.html">Spotnight Studios</a></li>
      <li><a href="http://spotnight.io/shop.html">Spotnight Shop</a></li>
        <br>
      <li><a href="http://spotnight.io/contact.html">Contact Us</a></li>
    </ul>
  </nav>
  <main>
    <h1>Contact us! :)</h1>
    <h5>Spotnight</h5>
    <div>
      <form action="send.php" method="POST">
        <input type="text" name="first_name" placeholder="Your first name, eg: 'John'">
        <input type="text" name="last_name" placeholder="Your last name, eg: 'Smith'">
        <input type="email" name="email" placeholder="Your Email, eg: 'yourname@example.com'">
        <select name="subj" size="1">
          <option value="new_website">I need a website</option>
          <option value="new_game">I want a game</option>
          <option value="new_employee">I want to be part of the team</option>
          <option value="new_employee_game">I want to participate making games</option>
          <option value="new_employee_site">I want to participate building sites</option>
          <option value="new_artist">I'm an artist and need help</option>
          <option value="something_else">Something else!</option>
        </select>
        <textarea name="message" placeholder="Your Message Goes Here!"></textarea>
        <input type="submit" value="Send!">
      </form>
    </div>
  </main>
  <footer>
    &copy; <span id="year">2015</span> : <a href="http://hybon.spotnight.io/">Spotnight.io</a> : Proudly created by Hybon. 
  </footer>
</body>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="script.js"></script>
</html>

我的send.php文档内容如下:

<!doctype html>
<html>
<head>
</head>
<body>
  <?php
    $first_name = $_POST["first_name"];
    $last_name = $_POST["last_name"];
    $email = $_POST["email"];
    $subj = $_POST["subj"];
    $message = $_POST["message"];
    $formcontent="From: " . $first_name . $last_name . ", " . $email . "\n Subject: " . $subj . "\n Message: " . $message;
    $recipient = "julian-avar@spotnight.io";
    $subject = "Contact Form - " . $subj . " - Spotnight.io";
    $mailheader = $email . " has tryed to reach you! \r\n";

    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Thank You! Your message has been recived, we&apos;ll answer as soon as possible!" . $first_name . $last_name . " -" . "<a href="contact.html">Go Back!</a>";
  ?>
</body>
</html>

这都是字面意思,意思是,代码与那里完全一样。我很难理解它,而且我仍然不明白它是如何工作的。

因此,如果您是超级棒的 php 专家,请帮助我理解并修复此代码,以便我稍后处理它,以便其他人了解它是如何工作的。

我找了很多地方,但是世界档案中没有看到最简单的联系方式。非常感谢!

您的 'send.php' 脚本将 html 与 php 混合,因此除非您使用输出缓冲,否则在尝试发送电子邮件时会遇到错误 html内容已发送至 client/browser。我建议 send.php 脚本只是纯粹的 php。此外,mailheader 变量 - 这对我来说看起来不对 - 我认为它至少应该包含 From: email@domain.com 字段。来自 PHP 手册:

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);





  <?php
    /* send.php */
    $first_name = $_POST["first_name"];
    $last_name = $_POST["last_name"];
    $email = $_POST["email"];
    $subj = $_POST["subj"];
    $message = $_POST["message"];


    $formcontent="From: " . $first_name . $last_name . ", " . $email . "\n Subject: " . $subj . "\n Message: " . $message;
    $recipient = "julian-avar@spotnight.io";
    $subject = "Contact Form - " . $subj . " - Spotnight.io";
    $mailheader = "From: ".$email . "\r\n";

    $res = mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

    if( $res ) header( 'location:/index.php?mailsent=true' );
    else header( 'location:/index.php?mailsent=false' );
   ?>
echo "Thank You! Your message has been recived, we&apos;ll answer as soon as possible!" . $first_name . $last_name . " -" . "<a href="contact.html">Go Back!</a>";

问题来了。 SO 的 code highliter 因为它而严重突出了它。

改用这个:

echo /* part of code missing */ $first_name . $last_name . " -" . "<a href='contact.html'>Go Back!</a>";
//                                                    mind the ' signs here ^^^^^^^^^^^^^

如果不转义 "'s 或使用单引号(我可能用错了术语)符号,则不能在另一个引号中包含一个引号。