如何在不离开调用它的实际页面的情况下使用 PHP 邮件功能
我正在为我正在构建的网站使用 html 注册表单。该表格接受名字、姓氏、电子邮件地址、出生日期(月、日、年)和性别。它调用一个 php 文件来处理结果并将它们通过电子邮件发送给我,如下所示:
<form method="post" action="sendMail.php" >
php 文件如下所示:
<?php
$firstName = $_REQUEST['FirstName'] ;
$lastName = $_REQUEST['LastName'] ;
$email = $_REQUEST['EmailAddress'] ;
$birthdate = $_REQUEST['month'] ;
$sex = $_REQUEST['sex'] ;
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "[email protected]", "Sign-Up Form Results", $message, "From: $email" );
/*?> header( "Location: http://www.example.com/thankyou.html" );
<?php */
?>
该脚本可以很好地处理和通过电子邮件发送结果,但我不知道如何制作“php mail() 函数”返回到完全相同的调用页面。我知道人们通常喜欢显示感谢页面,但我不想仅引导访问者进入感谢页面。有没有办法改变“php mail() 函数”以重定向到调用它的同一页面,或者在不离开页面的情况下调用它?感谢您提前提供任何提示!
更新 我已将 php 包含在与注册表单相同的页面中。它没有返回任何错误,但我似乎没有收到任何电子邮件发送到我为其设置的 gmail 帐户。我不知道将表单放在 html 文件中是否会更好,就像我通常所做的那样(在 php 函数之外)或放在 else 语句中,所以我尝试了两种方法,但没有给出电子邮件。下面的代码有逻辑错误吗?
版本1:
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "[email protected]", "Sign-Up Form Results", $message, "From: $email" );
echo "Thank you for using our mail form";
}
?>
名字:*
姓氏:*
电子邮件地址:*
生日: 月 一月 二月 行进 四月 可能 六月 七月 八月 九月 十月 十一月 十二月 天 年 函数更改日期(i){ var e = document.getElementById('day'); while(e.length>0) e.remove(e.length-1); 变量j=-1; 如果(i==“na”) k=0; 否则如果(i==2) k=28; 否则 if(i==4||i==6||i==9||i==11) k=30; 别的 k=31; 而(j++1909){ var s = document.createElement('选项'); var e = document.getElementById('年份'); s.text=y; s.value=y; 尝试{ e.add(s,null);} 抓住(前){ e.add(s);}}
性别: 男性 女性
*必填字段
版本2:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$firstName = $_REQUEST['FirstName'] ;
$lastName = $_REQUEST['LastName'] ;
$email = $_REQUEST['EmailAddress'] ;
$birthdate = $_REQUEST['month'] ;
$sex = $_REQUEST['sex'] ;
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "[email protected]", "Sign-Up Form Results", $message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form action='' method='post'>
<p>First Name:*
<input name='FirstName' type='text' id='FirstName' size='25' /> <br />
Last Name:*
<input name='LastName' type='text' id='LastName' size='25'/><br />
<br />
Email Address:*
<input name='EmailAddress' type='text' id='EmailAddress' size='25' onblur='valid_email=validateEmail(email_field,mandatory,messages);' />
<br />
<br />
Birthday:
<select name='month' onChange='changeDate(this.options[selectedIndex].value);'>
<option value='na'>Month</option>
<option value='1'>January</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>June</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name='day' id='day'>
<option value='na'>Day</option>
</select>
<select name='year' id='year'>
<option value='na'>Year</option>
</select>
<script language='JavaScript' type='text/javascript'>
function changeDate(i){
var e = document.getElementById('day');
while(e.length>0)
e.remove(e.length-1);
var j=-1;
if(i=='na')
k=0;
else if(i==2)
k=28;
else if(i==4||i==6||i==9||i==11)
k=30;
else
k=31;
while(j++<k){
var s=document.createElement('option');
var e=document.getElementById('day');
if(j==0){
s.text='Day';
s.value='na';
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
else{
s.text=j;
s.value=j;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}}}
y = 2010;
while (y-->1909){
var s = document.createElement('option');
var e = document.getElementById('year');
s.text=y;
s.value=y;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
</script>
<br />
</p>
<p>Gender:
<input type='radio' name='sex' value='male' /> Male
<input type='radio' name='sex' value='female' /> Female
<br />
<div id='mailListNote'>
*required fields
</div>
<br />
<br />
<br />
<input type='submit' name='submit' value='Submit' onclick='valid_email =validateEmail(email_field,mandatory,messages); '/>
<INPUT TYPE=RESET onClick='return confirm('Are you sure you want to reset the form?')'>
</p>
</form>";
}
?>
I am using an html sign up form for a website I'm building. The form accepts first name, last name, email address, birthdate (month,day,year), and sex. It calls a php file to process the results and email them to me like this:
<form method="post" action="sendMail.php" >
the php file looks like:
<?php
$firstName = $_REQUEST['FirstName'] ;
$lastName = $_REQUEST['LastName'] ;
$email = $_REQUEST['EmailAddress'] ;
$birthdate = $_REQUEST['month'] ;
$sex = $_REQUEST['sex'] ;
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "[email protected]", "Sign-Up Form Results", $message, "From: $email" );
/*?> header( "Location: http://www.example.com/thankyou.html" );
<?php */
?>
The script works fine to process and email the results, but I do not know how to make the 'php mail() function' return to the exact same calling page. I know that typically people like to show a thank you page but I do not want to lead the visitor to just a thank you page. Is there a way to alter the 'php mail() function' to redirect to the same exact page that called it, or to call it without leaving the page in the first place? Thanks for any hints in advance!
Update I have included the php into the same page with the sign up form. It isn't returning any errors but I do not seem to be getting any emails delivered to the gmail account I set up for it. I did not know if it would work better to place the form in the html file as I normally would (outside of the php function) or inside an else statement so I tried it both ways with neither giving an email. Was there a logical error in the code below ?
Version 1:
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "[email protected]", "Sign-Up Form Results", $message, "From: $email" );
echo "Thank you for using our mail form";
}
?>
First Name:*
Last Name:*
Email Address:*
Birthday:
Month
January
February
March
April
May
June
July
August
September
October
November
December
Day
Year
function changeDate(i){
var e = document.getElementById('day');
while(e.length>0)
e.remove(e.length-1);
var j=-1;
if(i=="na")
k=0;
else if(i==2)
k=28;
else if(i==4||i==6||i==9||i==11)
k=30;
else
k=31;
while(j++1909){
var s = document.createElement('option');
var e = document.getElementById('year');
s.text=y;
s.value=y;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
Gender:
Male
Female
*required fields
Version 2:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$firstName = $_REQUEST['FirstName'] ;
$lastName = $_REQUEST['LastName'] ;
$email = $_REQUEST['EmailAddress'] ;
$birthdate = $_REQUEST['month'] ;
$sex = $_REQUEST['sex'] ;
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "[email protected]", "Sign-Up Form Results", $message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form action='' method='post'>
<p>First Name:*
<input name='FirstName' type='text' id='FirstName' size='25' /> <br />
Last Name:*
<input name='LastName' type='text' id='LastName' size='25'/><br />
<br />
Email Address:*
<input name='EmailAddress' type='text' id='EmailAddress' size='25' onblur='valid_email=validateEmail(email_field,mandatory,messages);' />
<br />
<br />
Birthday:
<select name='month' onChange='changeDate(this.options[selectedIndex].value);'>
<option value='na'>Month</option>
<option value='1'>January</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>June</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name='day' id='day'>
<option value='na'>Day</option>
</select>
<select name='year' id='year'>
<option value='na'>Year</option>
</select>
<script language='JavaScript' type='text/javascript'>
function changeDate(i){
var e = document.getElementById('day');
while(e.length>0)
e.remove(e.length-1);
var j=-1;
if(i=='na')
k=0;
else if(i==2)
k=28;
else if(i==4||i==6||i==9||i==11)
k=30;
else
k=31;
while(j++<k){
var s=document.createElement('option');
var e=document.getElementById('day');
if(j==0){
s.text='Day';
s.value='na';
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
else{
s.text=j;
s.value=j;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}}}
y = 2010;
while (y-->1909){
var s = document.createElement('option');
var e = document.getElementById('year');
s.text=y;
s.value=y;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
</script>
<br />
</p>
<p>Gender:
<input type='radio' name='sex' value='male' /> Male
<input type='radio' name='sex' value='female' /> Female
<br />
<div id='mailListNote'>
*required fields
</div>
<br />
<br />
<br />
<input type='submit' name='submit' value='Submit' onclick='valid_email =validateEmail(email_field,mandatory,messages); '/>
<INPUT TYPE=RESET onClick='return confirm('Are you sure you want to reset the form?')'>
</p>
</form>";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以定义一个空白操作,表单将提交到其托管的同一 URL,如下所示:
You can define a blank action, and the form will submit to the same URL that it's hosted on, like so:
根据表单页面的编码方式,您可以将问题中列出的确切 PHP 放在表单页面本身的顶部,并将其包装在“if”语句中,以确定表单是否正在提交。例如:
然后您将按照之前的答案中提到的方式提交表单。
这样,您可以使用相同的 if 语句来显示反馈或某种形式的感谢。更进一步,这种方法允许您在提交的数据未通过验证过程时轻松提供错误。
Depending on how your form page is coded, you could put the exact PHP you listed in your question at the top of the form page itself and wrap it in an 'if' statement that determines if the the form is being submitted. e.g.:
Then you would have the form submit as mentioned in a previous answer.
This way, you could use the same if statement to display feedback or a thank you of some kind. To take this even further, this approach easily allows you to provide errors in the event that the submitted data fails your validation process.
使用ajax无需离开页面即可发送邮件。
Use ajax to send the mail without leaving the page.
您基本上有两个选择:
header("location:…");
中的 url 以返回原始页面。在提交表单后向用户提供一些反馈绝对是个好主意。因此,感谢页面可能是最用户友好的解决方案。甚至为您提供技术上最简单的解决方案。
希望这有帮助!
You have basically two options:
header("location:…");
to return to the original page.It is definitely a good idea to provide some feedback to the user after submitting your form. So, the thank you page might just be the most user-friendly solution. And even the technically easiest solution for you.
Hope this helps!