简体   繁体   中英

PHP contact form email random confirmation number to user

I have a php contact form, and when the user submits the form, both me and the user is sent a confirmation email. What I would like to do is generate a random confirmation number, and email to both the user and me for reference. Here is the code that I have to generate the random number:

 <p>Your confirmation number is: <strong><?php echo mt_rand(100000,999999);?></strong>. Keep this for your records.</p>

The number displays on the form, but my problem is that I don't know how to output this number so that it can be emailed. Please forgive me I'm not a programmer, but I do have some very basic php knowledge. So if someone can help me out with this I'd greatly appreciate it.

Here is the form on my school website: http://www.inrf.uci.edu/facility/services/service-request/

Here is also the some php code from my form:

    // name
    if(trim($_POST['contactName']) === '') {
        $nameError = '<span class="error">Please enter your name.</span>';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }
    // email
    if(trim($_POST['email']) === '')  {
        $emailError = '<span class="error">Please enter your email address.</span>';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = '<span class="error">You entered an invalid email address.</span>';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }



    // body ------------------------------------------
    if(!isset($hasError)) {
        $emailTo = get_option('tz_email');
        if (!isset($emailTo) || ($emailTo == '') ){
            $emailTo = 'myemail@email.com';
        }
        $subject = '[Foundry Form] From '.$name;

        $body .= "Name: $name \n\n";
        $body .= "Email: $email \n\n";
        $body .= "Phone: $phone \n\n";

        $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: . $email';
        $headers .= "\r\n" . 'BCC: myotheremail@email.com' . "\r\n";

        wp_mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }




  }


}


?>


<div id="bodyPage" class="clear">
<!------- start body ------->
<section id="contentPage">
  <h1 class="title"><?php the_title(); ?></h1>


 <?php if(isset($emailSent) && $emailSent == true) { ?>
        <div class="thanks">
          <p><strong>Thank You for Submitting a Service Request!</strong></p>
          <p>An INRF Staff Member will contact you shortly.</p>
          <p>If you do not receive a confirmation e-mail within 1-2 business days, you may contact us directly at (949) 824-2819 or <a href="mailto:info@inrf.uci.edu">info@inrf.uci.edu</a>.</p>
          <p>We look forward to serving your research needs!</p>


           <hr style="border:1px dashed #CCC;" />
          <?php
             echo "<Strong>Your Service Request information</strong><br>";
             echo "(You may print this page for your records)<br><br>";
             echo "<u>Contact Information</u><br>";
             echo "Name: ".$name;
             echo "<br>";
             echo "Email: ".$email;
             echo "<br>";

?>

        </div>
        <?php } else { ?>
        <?php the_content(); ?>
        <?php if(isset($hasError) || isset($captchaError)) { ?>
        <p class="error">Sorry, an error occured. See below.<p>

          <?php } ?>


           <?php if($captchaError != '') { ?><span class="error"><?=$captchaError;?></span><?php } ?>


     <script type="text/javascript">var RecaptchaOptions = { theme : 'white'};</script>

        <form action="<?php the_permalink(); ?>" id="foundryForm" method="post">
          <strong>Contact</strong> <br />
          <br />
          <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><label for="contactName">*Name:</label></td>
              <td><input type="text" size="40" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
                <?php if($nameError != '') { ?>
                <span class="error">
                <?=$nameError;?>
                </span>
                <?php } ?></td>
            </tr>
            <tr>
              <td><label for="email">*Email:</label></td>
              <td><input type="text" size="40" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
                <?php if($emailError != '') { ?>
                <span class="error">
                <?=$emailError;?>
                </span>
                <?php } ?></td>
            </tr>


          </table>


    <br /><br /> 

      <?php
          require_once('scripts/recaptchalib.php');
          $publickey = "6LfR0eESAAAAAFtSdYmpqqVnVwP8ZMHCr--BIu5f"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>

          <p><input type="submit"></input></p>
          <input type="hidden" name="submitted" id="submitted" value="true" />
        </form>
        <?php } ?>      

The first thing you should do is create the confirmation number before you send the email:

$confirmationNumber = mt_rand(100000,999999);

Then add this to the body of the email:

$body .= "Confirmation Number: $confirmationNumber\n\n";

Then use $confirmationNumber when displaying on the page:

<p>Your confirmation number is: <strong><?php echo $confirmationNumber; ?></strong>. Keep this for your records.</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM