联系表单响应出现在主页面上

时间:2014-04-23 09:30:39

标签: php html

我有一个带锚点的网页,联系表格是最后一部分。我在那里有一条回音信息,一旦我加载页面,就会出现在那里。任何想法如何使它只在邮件发送时出现?此外,如果页面已加载,它会自动发送邮件。感谢。

表单代码。

<form name="form1" id="ff" method="post" action="insert.php"> 
  <label>
    <span>Name*:</span>
    <input type="text" placeholder="Please enter your name" name="name" id="name" required>
  </label>

  <label>
    <span>Phone*:</span>
    <input type="tel" placeholder="Please enter your phone" name="phone" id="phone" required>
  </label>

  <label>
    <span>Email*:</span>
    <input type="email" placeholder="youremail@mail.com" name="email" id="email" required>
  </label>

  <label>
    <span>Message*:</span>
    <textarea rows="4" cols="55" name="comment" id="comment" placeholder="Purpose of contacting"></textarea>
  </label>

  <input class="sendButton" type="submit" name="Submit" value="Send">
  <?php 
  {
    if (isset($_POST['name'])) 
    // Get values from the form
    $name=$_POST['name'];
    $phone=$_POST['phone'];
    $email=$_POST['email'];
    $comment=$_POST['comment'];

    $to = "#";
    $subject = "New Mail From Website";
    $message = " Name: " . $name .  "\r\n Phone: " . $phone . "\r\n Email: " . $email . "\r\n Message: " . $comment;
    //$message = $comment;

    $from = "#";
    $headers = "From:" . $from . "\r\n";
    $headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; 

    if(@mail($to,$subject,$message,$headers,$email))
    {
      echo "<p class='contact-text'>Thank you for your email. I will contact you shortly!</p>";
    }else{
      echo "<p class='contact-text'>Error! Please try again.</p>";
    }
  } 
  ?>
</form>

1 个答案:

答案 0 :(得分:1)

使用此

<?php 
  if (isset($_POST['name'])) {
    // Get values from the form
    $name=$_POST['name'];
    $phone=$_POST['phone'];
    $email=$_POST['email'];
    $comment=$_POST['comment'];

    $to = "#";
    $subject = "New Mail From Website";
    $message = " Name: " . $name .  "\r\n Phone: " . $phone . "\r\n Email: " . $email . "\r\n Message: " . $comment;
    //$message = $comment;

    $from = "#";
    $headers = "From:" . $from . "\r\n";
    $headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; 

    if(@mail($to,$subject,$message,$headers,$email))
    {
      echo "<p class='contact-text'>Thank you for your email. I will contact you shortly!</p>";
    }else{
      echo "<p class='contact-text'>Error! Please try again.</p>";
    }
  } 
?>

(条件if (isset($_POST['name']))也包括消息)