PHP联系表格给用户发送随机确认号码
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:
我有一个php联系表单,当用户提交表单时,我和用户都会收到一封确认电子邮件。我想要做的是生成随机确认号码,并通过电子邮件发送给用户和我,以供参考。这是我必须生成随机数的代码:
<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.
数字显示在表单上,但我的问题是我不知道如何输出这个数字,以便可以通过电子邮件发送。请原谅我,我不是程序员,但我确实有一些非常基本的PHP知识。因此,如果有人可以帮助我,我会非常感激。
Here is the form on my school website: http://www.inrf.uci.edu/facility/services/service-request/
这是我学校网站上的表格:http://www.inrf.uci.edu/facility/services/service-request/
Here is also the some php code from my form:
这里也是我的表格中的一些PHP代码:
// 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 } ?>
1 个解决方案
#1
1
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:
然后在页面上显示时使用$ confirmationNumber:
<p>Your confirmation number is: <strong><?php echo $confirmationNumber; ?></strong>. Keep this for your records.</p>
更多相关文章
- 仅在LARAVEL或AJAX中提交表单时,无需用户身份验证即可将文件上载
- 使用GMail SMTP服务器从PHP页面发送电子邮件
- 来自php表单的样式电子邮件
- 请问如何用php实现表单提交后以邮件的形式把表单内容发到邮箱中
- 如何在jQuery Mobile页面中提交表单?
- Symfony2 -从数据库中提取数据并以表单形式显示
- laravel 框架自带表单验证
- 如何检查电子邮件id是否存在?
- AJAX学习之提交表单