1

我有一个简单的电子邮件发件人。它工作正常并发送电子邮件,但它在提交时向我发送了几份副本,几乎就像它的循环一样。我无法确定导致它循环的原因:

<div id="content">

    <?php 
    $email = $_POST['email'];
    $message = $_POST['message'];
    $firstName = $_POST['fname'];
    $lastName = $_POST['lname'];

    $errorForm = false;
    if(strlen($message) < 12){
        $errorForm = true;
        $errorMessage = true;
    }

    if(strlen($email) < 12){
        $errorForm = true;
        $errorMail = true;
    }

    if(strlen($firstName) < 3){
        $errorForm = true;
        $errorFname = true;
    }

    if(strlen($lastName) < 3){
    $errorForm = true;
        $errorLname = true;
    }

    if($errorForm == true ){?>

    <h3>Jane Doe</h3>
    <p>Owner, Founder &amp; Graphic Designer</p>
    <a href="mailto:info@example.com">info@example.com</a>

    <div id="contact">
            <form action="sender-contact.php" method="post">
              <fieldset>

                  <legend>There was an error in your submission. Please correct it.</legend>

                  <label for="fname">First Name</label>
                  <input type="text" size="20" maxlength="18" name="fname" id="fname" class="<?php if($errorFname == true){echo "error";}?>" value ="<?=$firstName?>"/>

                  <label for="lname">Last Name</label>
                  <input type="text" size="20" maxlength="18" name="lname" id="lname" class="<?php if($errorLname == true){echo "error";}?>"value ="<?=$lastName?>"/>

                  <label for="email"> eMail address</label>
                  <input type="text" size="40" maxlength="38" name="email" id="email" class="<?php if($errorMail == true){echo "error";}?>"value ="<?=$email?>"/>

                   <label for="message"> Your message</label>
                   <textarea name="message" id="message" rows="8" cols="60" class="<?php if($errorMessage == true) {echo "error"; }?>" ><?=$message?></textarea>

                  <input type="submit" value="Send" id="submit"  class="submit-button" />
              </fieldset>
          </form> 
    </div>

        <?php } 

        if($errorForm == false){

            $to = "info@example.com";
            $email = $_REQUEST['email'] ;
            $subject = "New Email from info@example.com" ;
            $message = $_REQUEST['message'] ;
            $headers = 'New message from '.$_POST['fname'].' '.$_POST['lname'].': \n';
            $headers .= $_POST['email'];
            mail($to, $subject, $message, $headers);
            ?>

         <?php
            echo ("<h3>\r\nWhen you buy from a small business, you aren't helping a CEO buy a 3rd vacation home. You are helping a little girl get dance lessons, a boy play hockey, a mom put food on the table, a dad pay a mortgage, or a student pay for college. Our customers are our neghbours and our shareholders...they are the ones we strive to make happy.<br/> <br/>Thank you for supporting small business and I'll get in touch with you within 72 hours!</h3>"); ?>

    <?php   } ?>
</div>
4

1 回答 1

1

在显示表单并运行 php 邮件功能之前,请尝试检查您的提交按钮是否被点击:

<?php

if (isset($_POST['theNameOfSubmitButton']))
{
    //create your variables from the form
      $email = $_POST['email'];
      $message = $_POST['message'];
      $firstName = $_POST['fname'];
      $lastName = $_POST['lname'];

      ....

    //send email
    if(mail("$to","$subject","$message","$headers"))
    {
    //if mail sends, say thank you
    echo ("<h3>\r\nWhen you buy from a small business, you aren't helping a CEO buy a 3rd vacation home. You are helping a little girl get dance lessons, a boy play hockey, a mom put food on the table, a dad pay a mortgage, or a student pay for college. Our customers are our neghbours and our shareholders...they are the ones we strive to make happy.<br/> <br/>Thank you for supporting small business and I'll get in touch with you within 72 hours!</h3>");

    // else, if mail fails, email web master, give "pretty" error message
      }
      else
      {
      echo "Message failed to send. sorry about that....";
      mail("webmaster@domain.com", "problem with email form", "fix this", From:admin@domain.com");
      }
    //else, the submit button was not clicked... display the form
    }

    <!-- code for form goes here -->

使用这种方法将有助于避免很多“奇怪的事情”

于 2012-06-27T11:56:40.577 回答