如何使用PHP邮件功能而不离开它所调用的实际页面

时间:2011-02-20 17:00:52

标签: php html email forms dynamic

我正在为我正在建设的网站使用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( "ty@ollielifestyle.com", "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( "ymaxwell86@gmail.com", "Sign-Up Form Results", $message, "From: $email" );  
echo "Thank you for using our mail form";
}
?>       
               

名字:*              
             姓:*              
             
             电子邮件地址:*                            
             
             生日:                              月                一月                二月                游行                四月                可以                六月                七月                八月                九月                十月                十一月                十二月                                            天                                            年                                 function changeDate(i){     var e = document.getElementById('day');     而(e.length大于0)     e.remove(e.length-1);     var j = -1;     如果(ⅰ== “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('option');     var e = document.getElementById('year');     s.text = Y;     s.value = Y;     尝试{     e.add(S,NULL);}     赶上(前){     e.add(一个或多个);}}                   
           

           

性别:               男               女              
                         *必填字段                          
    
             
                                                     

    

第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( "ymaxwell86@gmail.com", "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:* &nbsp; &nbsp; &nbsp;&nbsp;
         <input name='FirstName' type='text' id='FirstName' size='25' />&nbsp; &nbsp;<br />

         Last Name:*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input name='LastName' type='text' id='LastName' size='25'/><br />
         <br />
         Email Address:*&nbsp;
         <input name='EmailAddress' type='text' id='EmailAddress' size='25' onblur='valid_email=validateEmail(email_field,mandatory,messages);' />
         <br />
         <br />
         Birthday: &nbsp;&nbsp;&nbsp;
         <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:  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
         <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 />

         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input type='submit' name='submit' value='Submit' onclick='valid_email =validateEmail(email_field,mandatory,messages); '/>
         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
         <INPUT TYPE=RESET  onClick='return confirm('Are you sure you want to reset the form?')'>
       </p>
     </form>";
  }
?>

4 个答案:

答案 0 :(得分:3)

使用ajax发送邮件而不离开页面。

答案 1 :(得分:2)

您基本上有两种选择:

  • 只需更改header("location:…");中的网址即可返回原始页面。
  • 或使用Ajax在后台提交表单。有很多基于JQuery的有用插件。 Google is your friend.

提交表单后向用户提供一些反馈绝对是个好主意。所以,感谢页面可能只是用户友好的解决方案。甚至是技术上最简单的解决方案。

希望这有帮助!

答案 2 :(得分:0)

您可以定义一个空白操作,表单将提交到其托管的相同网址,如下所示:

<form action="" method="POST">

答案 3 :(得分:0)

根据表单页面的编码方式,您可以将问题中列出的确切PHP放在表单页面本身的顶部,并将其包装在确定表单是否正在提交的“if”语句中。 e.g:

if (isset($_REQUEST['EmailAddress'])) { /** your code here **/}

然后,您可以按照上一个答案中的说明提交表单。

<form action="" method="POST">

这样,您可以使用相同的if语句来显示反馈或某种感谢。更进一步,这种方法很容易让您在提交的数据未通过验证过程时提供错误。