php sendmail脚本中的未定义变量

时间:2014-08-28 14:59:44

标签: php variables undefined

我已经检查了论坛但是却无法解决这个问题。我收到一条错误消息“注意:未定义的变量:C:\ xampp \ htdocs中的错误....

我的代码:

    <?php
   if (isset($_POST["send"])) {
   $name = $_POST["name"];
   $email = $_POST["email"];
   $subject = $_POST["subject"];
   $message = $_POST["message"];

   $errors = array();

   $email_matcher = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*" .

   "@" .

   "[a-z0-9-]+" .

   "(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";

   if (preg_match($email_matcher, $email) == 0) {

     array_push($errors, "You did not enter a valid email address");
   }


   if (count($errors) == 0) {

    $to = "johnnyB@gmail.com"; 
    $subject = "[From My Web] " . $subject;
    $from = $name . " <" . $email . ">";
    $headers = "From: " . $from;
    header("location:thank_you.php");

    if (!mail($to, $subject, $message, $headers)) {
      array_push($errors, "Mail failed to send.");
       }
      }
     }

下面的表格代码

<form action="" method="POST">
<fieldset>
<legend>User Information</legend>
<label for="name">Name:</label>
<input type="text" name="name" id="name" required/>
<br />
<label for="pass">Password:</label>
<input type="password" name="password" id="password" required/>
<br />
<label for="email">Email:</label>
<input type="email" name="email" id="email" required/>
<br />
<label for="cwid">CWID:</label>
<input type="text" name="cwid" id="cwid" required/>
<br />
<label for="comp">Complete:</label>
<input type='submit' name='submit' value='Submit Registration'>
</fieldset>
</form>
</center>
</body>
</html>

任何帮助非常感谢!谢谢。

1 个答案:

答案 0 :(得分:0)

编辑(请参阅下面有关缺失错误数组和foreach循环的更多/修改的PHP代码。)

由于您尚未提供HTML表单,因此最可能的原因是您的表单元素未命名。

我正在为您提供一个示例HTML表单,您可以自行编写并修改以适应,我已使用您的问题中提供的PHP / mail函数对其进行了测试。

<form method="POST" action="handler.php">

    Name: 
    <input type="text" name="name">

    <br>

    Email: 
    <input type="text" name="email">

    <br>

    Subject: 
    <input type="text" name="subject">

    <br>
    Message: <br>

    <textarea rows="6" name="message" cols="40"></textarea>

    <br>

    <input type="submit" value="Submit" name="send">

</form>

编辑:使用foreach错误方法修改了PHP。

<?php 

   if (isset($_POST["send"])) {
   $name = $_POST["name"];
   $email = $_POST["email"];
   $subject = $_POST["subject"];
   $message = $_POST["message"];

   $errors = array();


   $email_matcher = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*" .

   "@" .

   "[a-z0-9-]+" .

   "(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";

   if (preg_match($email_matcher, $email) == 0) {

     array_push($errors, "You did not enter a valid email address");
   }


   if (count($errors) == 0) {

    $to = "email@example.com"; 
    $subject = "[From My Web] " . $subject;
    $from = $name . " <" . $email . ">";
    $headers = "From: " . $from;
    header("location:thank_you.php");


    if (!mail($to, $subject, $message, $headers)) {
      array_push($errors, "Mail failed to send.");
       }
      }


     }

?>


<?php foreach($errors as $error):?>
    <p class="layer-content">
    <?php echo $error;?>
    </p>
<?php endforeach;?>