php自我电子邮件表单问题

时间:2012-02-22 21:57:58

标签: php

我的php代码存在问题。我正在使用jquery来验证用户输入。当我点击提交按钮时,我收到了电子邮件,但我无法返回到另一个页面,例如“它已成功发送”。我是新手,我不知道如何获得帮助,所以我要发布它发送的php页面,我不能放弃它,所以T.T

此页面名为ajax.php


<?
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$telephone = stripslashes($telephone);
$message = stripslashes($message);
if(mail("mememe@hotmail.com","Email from $name","
$message

(From $name, $email, $telephone)","From: $email")){
    echo "$name $email $telephone $message";
}
echo "Email Successfully Sent!<br />
<br />
Name: $name<br />Email: $email<br />telephone: $telephone<br />Message: $message";
?>

此页面是带有jquery验证“onblur”的表单,我提交自

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0       Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Contact Us</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="../style.css" rel="stylesheet" type="text/css" />
    <script src="prototype.js" type="text/javascript"></script>
    <script src="livevalidation.js" type="text/javascript"></script>
    </head>
    <body>
    <div class="main">
     <div class="footer_resize">
      <div class="footer">
       <div class="menu">
        <ul>
            <li><a href="../index.html">Home </a></li>
            <li><a href="../services/services.html">Services</a></li>
            <li><a href="contact.php" class="active">Contact Us</a></li>
        </ul>
      </div>
      <div class="clr"></div>
     </div>
    </div>
    <div class="clr"></div>
    <div class="body">
    <div class="body_resize">
        <div class="left">
          <h2>Contact for appointment</h2>
          <p><a href="#"></a>
         <strong>Address</strong>
        <br />So Shiq Studio
        <br />380 King Street North
        <br />Waterloo ON
        <br />Tel. 519.721.8060     
        <br /></p>
          <p></p>
          <p><strong>Hours of Operation</strong><br />
            Tuesday: 10AM – 7PM <br />
            Friday: 10AM – 8PM <br />
            Saturday: 9AM – 3PM</p>

            <p></p>
          <p><strong>Private Appointments</strong><br />
            Monday - Wednesday <br />
            Colour Upon Consultation <br />
            *Minimum 2 Services*</p>

          <p><a href="#"></a><br />
        </p>
          <p>
          <a href="#"><strong></strong></a><br />
            </p>
      </div>
        <br /><br /><br />
        <form id="my-form" style="padding-left:16.9em" >

        <table class=shadow border="0" width="71%" style="background:#ececec; font:normal 12px Arial, Helvetica, sans-serif; color:#6b6b6b;" cellspacing="15">
            <tr align="left"><td><strong>Full Name</strong></td><td><input type="text" size="50" id="name" name="name" style="font:normal 12px Arial, Helvetica, sans-serif; color:#6b6b6b;"></td></tr>
            <tr align="left"><td><strong>Email Address</strong></td><td><input type="text" size="50" id="email" name="email" style="font:normal 12px Arial, Helvetica, sans-serif; color:#6b6b6b;"></td></tr>
            <tr align="left"><td><strong>Phone Number</strong></td><td><input type="text" size="50" id="telephone" name="telephone" style="font:normal 12px Arial, Helvetica, sans-serif; color:#6b6b6b;"></td></tr>
            <tr align="left"><td valign="top"><strong>Comments</strong></td><td><textarea id="message" name="message" rows="8" cols="80" style="font:normal 12px Arial, Helvetica, sans-serif; color:#6b6b6b;"></textarea></td></tr>
            <tr align="left"><td>&nbsp;</td><td>
            <input type="button" name:"clear" value="Send" onclick="sendRequest();"  style="font:normal 12px Arial, Helvetica, sans-serif; color:#6b6b6b;font-weight:bold;"/></td></tr>
        </table>

        </form>

        <script type="text/javascript">
            var name = new LiveValidation( 'name' );
            name.add( Validate.Presence );

            var email = new LiveValidation( 'email' );
            email.add( Validate.Presence );
            email.add( Validate.Email );

            var telephone = new LiveValidation( 'telephone' );
            telephone.add( Validate.Presence );
            telephone.add( Validate.Telephone );

            var message = new LiveValidation( 'message' );
            message.add( Validate.Presence );

            function sendRequest(){
                if(LiveValidation.massValidate( [ name, email, telephone, message ] )){
                    new Ajax.Request('ajax.php',
                    {
                        method:'post',
                        parameters: $('my-form').serialize(true),
                        onLoading: function(){

                            $('update_div').show();
                            $('update_div').innerHTML = "Sending...";
                        },
                        onSuccess: function(transport){
                            var response = transport.responseText || "No response text";
                            $('update_div').innerHTML = response;


                        },
                        onFailure: function(){ 
                            $('update_div').innerHTML = "Something went wrong...";
                        }
                    });
                }
            }
        </script>
        <br /><br /><br /><br />
        <div class="clr"></div>
    </div>
  </div>
  <div class="clr"></div>
  <div class="clr"></div>
  <div class="footer">
     <div class="footer_resize">
      <p class="leftt">
         <a href="https://twitter.com/#!/atmosphair"><img src="../images/rss_1.gif" alt="picture" width="18" height="16" border="0" /></a>
        <a href="https://www.facebook.com/pages/Atmosphair-Salon/132172773469260"><img src="../images/rss_2.gif" alt="picture" width="18" height="16" border="0" /></a>
      </p><p class="right"> <a href="#">© Copyright COSMO STEFAN All Rights Reserved&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></p>
          <div class="clr"></div>
        </div>
         <div class="clr"></div>
      </div>
    </div>
    </body>
    </html>
我已经尝试了下面的建议,但我无法让它工作TT我知道代码是凌乱的,我真的很新,只是想弄清楚我做错了什么..非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

更新

使用标题()

要使用PHP重定向页面,请使用header()功能。此功能旨在将HTTP标头发送回客户端。如果您使用值发送HTTP Location标头,则客户端将或多或少地将其解释为“转到值中指定的页面”。

使用标题功能重定向:

<?php  
    header('Location: http://www.yoursite.com/the/page/you/want/to/go/to.php?success=1'); 
    exit(); 
?>

请注意,我在发出对exit()的调用后明确说出header()。除非您退出,否则标题重定向后的任何代码仍将执行

使用$ _GET

_GET是PHP中的superglobal。当PHP加载时,它会将在URL中找到的所有变量加载到此关联数组中。上面的示例将名为GET的{​​{1}}变量设置为1.您可以像这样访问success超全局:

GET

您可以使用此功能通过检查是否已设置echo( $_GET['success'] ); 并且等于1来告诉您的主叫页面您的电子邮件已发送,然后输出您的消息。

$_GET['success']

有更复杂的方法可以做到这一点,但我相信这可能是最简单,最快捷的方法。在发出对<?php if(isset($_GET['success']) && $_GET['success'] == 1) echo( 'Email Successfully Sent!<br />' ); ?> 函数的调用之前,请确保输出任何内容。

答案 1 :(得分:0)

我在下面整理了你的代码。它应该工作

<?
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$telephone = stripslashes($telephone);
$message = stripslashes($message);
if(mail("mememe@hotmail.com","Email from". $name.$message))
{
echo "Email Successfully Sent!<br /><br />Name:". $name."<br />Email:". $email."<br />telephone:". $telephone."<br />Message:". $message;
}
?>

如果您想重定向到成功页面,这就是代码

<?
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$telephone = stripslashes($telephone);
$message = stripslashes($message);
if(mail("mememe@hotmail.com","Email from". $name.$message))
{
//echo "Email Successfully Sent!<br /><br />Name:". $name."<br />Email:". $email."<br />telephone:". $telephone."<br />Message:". $message;
header("location:http://www.yourdomain.com/success.html");
}
?>