电子邮件表单中的未定义变量(消息变量)

时间:2015-03-30 12:31:57

标签: php forms twitter-bootstrap email contact

我正在建立一个基于bootstrap 3的网站,我要求它有一个联系表格。代码是字面上的复制和粘贴,但已经适应我的使用。这很简单,不幸的是,我找不到我用过它的参考。下面是HTML代码的摘录

<div class="container">
      <div class="row">
        <div class="col-md-4">
        <h3>Message</h3><hr>
        <form name="contactform" method="post" action="js/contact.php">
        <div class="input-group">
        <input type="text" class="form-control" placeholder="Name" aria-describedby="name">
        </div>

        <br>

        <div class="input-group">
        <input type="email" class="form-control" placeholder="Email" aria-describedby="email">
        </div>

        <br>

        <div class="input-group">
        <input type="tel" class="form-control" placeholder="Phone" aria-describedby="phone">
        </div>

        <br>

        <div class="input-group">
        <input type="text" class="form-control" placeholder="Message" aria-describedby="message">
        </div>

        <br>

        <div class="input-group-btn">
        <input class="btn btn-default" type="submit" value="Submit"><a href="js/contact.php"></a>
        </div>

        </form>
        </div>

以下是使用的PHP脚本;

<?php
if (isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "fullbodyfitness@outlook.com";
    $email_subject = "enquiry";

    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['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['message'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $first_name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['phone']; // not 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,$name)) {
      $error_message .= 'The Name you entered does not appear to be valid.<br />';
    }
  }

  if (strlen($message) < 2) {
    $error_message .= 'The Message 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 .= "Name: ".clean_string($name)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "Phone: ".clean_string($phone)."\n";
  $email_message .= "Message: ".clean_string($message)."\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); 

  <!-- include your own success html here -->
  Thank you for contacting us. We will be in touch with you very soon.
?>

2 个答案:

答案 0 :(得分:1)

变化

 $comments = $_POST['message']; // required 

$message = $_POST['message']; // required

声明$ message变量

 // EDIT THE 2 LINES BELOW AS REQUIRED 
$email_to = "fullbodyfitness@outlook.com";
$email_subject = "enquiry"; 
$message = '';

答案 1 :(得分:0)

看起来好像你没有在HTML中命名你的输入,所以这些都没有在后期数据中传播。

应该是......

<input type="email" name="email" class="form-control" placeholder="Email" aria-describedby="email">

后期数据由数组中的命名键值对组成 - 其中键是要发布的表单中输入的“名称”,值是该输入的数据。

你还需要改变:

$comments = $_POST['message'];

$message = $_POST['message'];