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

时间:2015-03-03 10:50:56

标签: php html forms

我正准备启动我的网站,但首先我需要填写联系表单,以确保用户可以与我联系。我不是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专家,请帮助我理解并修复这些代码,以便我以后可以处理它,以便其他人了解它的工作原理。

我搜索了很多地方,但最简单的联系表格并没有出现在世界档案馆中。非常感谢你!

2 个答案:

答案 0 :(得分:0)

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的代码highliter突出了它。

请改用:

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

您不能在不转义“或使用单引号(我可能在术语上有误)标志”的情况下在另一个引用中包含引号。

答案 1 :(得分:-1)

你的'send.php'脚本将html与php混合在一起,所以除非你使用输出缓冲,否则当你因为html内容已经被发送到客户端/浏览器而尝试发送电子邮件时会遇到错误。我建议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' );
   ?>