PHP联系表格不起作用 - 请指教

时间:2014-03-29 00:13:00

标签: php forms contact

我正在为一个站点开发HTML联系表单,该站点在单击Submit时执行PHP代码文件。我遇到的问题是PHP脚本显示"消息无法发送。"我以前在另一个站点中使用过这个确切的PHP文件,所以问题可能在于HTML页面。请提供任何建议。

HTML - contact.html(不包含标题)

<body>

<header>
  <img src="images/Banner" />
</header>

<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="badges.html">Badges</a></li>
<li><a href="news.html">News</a></li>
<li><a href="calendar.html">Calendar</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li><a href="bio.html">Bio</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>        
</nav>

<article>
<div id="pagebanner"><div id="bannertext"><!-- InstanceBeginEditable name="Page Banner" --  >CONTACT ME<!-- InstanceEndEditable --></div></div><!-- InstanceBeginEditable name="Page Content" --  >
<div id="pageintro">If you need to contact me, please use one of the following ways.</div>
<div class="bodytextbold" style="text-align: left;">EMAIL</div>
<div class="bodytext" style="text-align: left;">Please complete the following form and click Submit to send me a message.</div><form id="contactform" method="post" action="contact.php">
  <p>
    <label for="textfield">Name:</label>
    <input name="name" type="text" id="name" size="35">
    <label for="textfield2">    </label>
    <label for="email">Email:</label>
    <input name="email" type="text" id="email" size="25">
    <label for="textfield2"><br>
      Subject:</label>
    <input type="text" name="subject" id="subject">
  </p>
  <p>
    <label for="textarea">Message:</label></p>
  <p>
    <textarea name="message" cols="75" rows="15" id="message"></textarea>
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit">
    <input type="reset" name="reset" id="reset" value="Clear">
  </p>
</form>
<div class="bodytext">THANK YOU FOR YOUR MESSAGE.  YOU WILL RECEIVE A RESPONSE SOON!</div>
<div class="bodytextbold" style="text-align: left;">PHONE</div>
<div class="bodytext" style="text-align: left;">You may call me at <span style="font-weight:  bold";>(number)</span> any time between 5 PM and 9 PM.</div>
<!-- InstanceEndEditable -->
</article>

<footer>

<div style="margin: 0 0 15px 0;"><a href="index.html">Home</a> | <a  href="badges.html">Badges</a> | <a href="news.html">News</a> | <a href="gallery.html">Gallery</a> |   <a href="bio.html">Bio</a> | <a href="contact.html">Contact</a></div>
Copyright &copy; 2014 Paradimensional Entertainment
</footer>
</body>
<!-- InstanceEnd --></html>

PHP - contact.php

<body>
<?php 
$to = "someone@email.com"; 
$subject = $_REQUEST['subject'] ; 
$name= $_REQUEST['name'] ;
$email = $_REQUEST['email'] ; 
$message = $_REQUEST['message'] ; 
$headers = "From: $name, $email" ; 
$sent = mail($to, $subject, $message, $headers) ; 
if($sent) {
echo ('Your message was successfully sent.');}
else {
echo ('Your message failed to send.  Please try again.');}
?> 

1 个答案:

答案 0 :(得分:0)

HTML - contact.html

<!DOCTYPE html>
<html lang="en">
  <head>
   <meta charset="utf-8">
   <title>Form Contact</title>
  </head>
  <body> 
   <header>
    <img src="images/Banner" />
   </header>
   <nav>
    <ul>
     <li><a href="index.html">Home</a></li>
     <li><a href="badges.html">Badges</a></li>
     <li><a href="news.html">News</a></li>
     <li><a href="calendar.html">Calendar</a></li>
     <li><a href="gallery.html">Gallery</a></li>
     <li><a href="bio.html">Bio</a></li>
     <li><a href="contact.html">Contact</a></li>
    </ul>        
   </nav>

   <article>
    <div id="pagebanner">
     <div id="bannertext">
      <div id="pageintro">If you need to contact me, please use one of the following ways.</div>
      <div class="bodytextbold" style="text-align: left;">EMAIL</div>
      <div class="bodytext" style="text-align: left;">Please complete the following form and click Submit to send me a message.</div>
       <form id="contactform" method="post" action="contact.php">
        <p>
         <label for="textfield">Name:</label>
         <input name="name" type="text" id="name" size="35">
         <label for="textfield2">    </label>
         <label for="email">Email:</label>
         <input name="email" type="text" id="email" size="25">
         <label for="textfield2"><br>Subject:</label>
         <input type="text" name="subject" id="subject">
        </p>
        <p>
         <label for="textarea">Message:</label>
        </p>
        <p>
         <textarea name="message" cols="75" rows="15" id="message"></textarea>
        </p>
        <p>
         <input type="submit" name="submit" id="submit" value="Submit">
         <input type="reset" name="reset" id="reset" value="Clear">
        </p>
       </form>

       <div class="bodytext">THANK YOU FOR YOUR MESSAGE.  YOU WILL RECEIVE A RESPONSE SOON!</div>
       <div class="bodytextbold" style="text-align: left;">PHONE</div>
       <div class="bodytext" style="text-align: left;">You may call me at <span style="font-weight:  bold";>(number)</span> any time between 5 PM and 9 PM.</div>
     </div>
   </div>
  </article>
  <footer>
   <div style="margin: 0 0 15px 0;">
    <a href="index.html">Home</a> | <a  href="badges.html">Badges</a> | <a href="news.html">News</a> | <a href="gallery.html">Gallery</a> |  <a href="bio.html">Bio</a> |     <a href="contact.html">Contact</a>
   </div>
   Copyright &copy; 2014 Paradimensional Entertainment
  </footer>
 </body>
</html>

PHP - contact.php

 <?php 
  echo $_POST['name'];
  echo " - ";
  echo $_POST['email'];
  echo " - ";
  echo $_POST['message'];

  $to = "someone@email.com"; 
  $subject = $_POST['subject'] ; 
  $name= $_POST['name'] ;
  $email = $_POST['email'] ; 
  $message = $_POST['message'] ; 
  $headers = "From: $name, $email" ; 
  $sent = mail($to, $subject, $message, $headers) ; 
  if($sent) {
    echo ('Your message was successfully sent.');}
  else {
   echo ('Your message failed to send.  Please try again.');}
 ?>