2012-07-20 157 views
0

我想发送电子邮件使用jQuery和PHP。发送电子邮件从PHP和jQuery

我的HTML代码

<form id="contactform"> 
      <table border="0" cellpadding="0" cellspacing="0" width="186"> 
       <tbody> 
       <tr> 
        <td bgcolor="#2A677E" width="25">&nbsp;</td> 
        <td bgcolor="#2A677E" width="161">&nbsp;</td> 
       </tr> 
       <tr> 
        <td bgcolor="#2A677E">&nbsp;</td> 
        <td bgcolor="#2A677E"><img src="images/icon-email.jpg" alt="" align="left" height="17" hspace="2" width="27"><span class="text-white">Email this page</span></td> 
       </tr> 
       <tr> 

        <td bgcolor="#2A677E">&nbsp;</td> 


        <td bgcolor="#2A677E"> 
         <input name="email" onFocus="this.value=''" onMouseOver="window.status='Enter email address here and tell a friend about this site...'; return true" onMouseOut="window.status='';return true" size="22" type="text" class="button required email"> 
         <table border="0" cellpadding="0" cellspacing="0" width="80%"> 
         <tbody> 
          <tr> 
          <td align="right" height="30"><input class="button-dark" onMouseOver="window.status='Click to send an email (with this page address) to a friend! Enter email address above...'; return true" onMouseOut="window.status='';return true" value="Send" type="submit"> 


          </td> 


<div class="saving" style="background-color:#D1F4DA;border:1px solid #017F1F;display:none;width:175px; font-size:12px; padding:5px;text-align:center; margin-left:-25px; margin-top:0px; position:absolute;"><img src="validate/ajax-loader.gif"/> Please Wait Processing Request </div> 
           <div class="success" style="background-color:#D1F4DA;border:1px solid #017F1F;display:none;width:175px; font-size:12px;padding:5px;text-align:center; margin-left:-25px; margin-top:0px; position:absolute;"> <img src="validate/validYes.png"/> Message Sent.<br /> 
           Thanks.</div> 
           <div class="nosuccess" style="background-color:#ffebe8;border:1px solid #dd3c10;display:none;width:175px; font-size:12px;padding:5px;text-align:center; margin-left:-25px; margin-top:0px; position:absolute;"> <img src="validate/failed.png"/> Error Sending Email. </div>       


          </tr> 

         </tbody> 
        </table> 
        </form> 

我有以下的jQuery代码。

<script src="validate/jquery.js" type="text/javascript"></script> 
<script id="demo" type="text/javascript"> 
$(document).ready(function() { 
     var validator = $("#contactform").validate({ 
     submitHandler: function() { 

     $('div.sending').fadeIn('slow'); 
     var a  = <?php echo $_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]; ?>; 
     var email  = $('#email').attr('value'); 

      $.ajax({ 
      type: "POST", 
      url: "send_page_friend.php", 
      data: "a="+ a +"&email="+ email, 
      success: function(html){ 

       if (html==1){ 
       $('div.saving').hide(); 
       $('div.success').fadeIn(); 
       setTimeout(function() { 
       $('div.success').fadeOut('slow'); 

       }, 3000); 
       } 

       if (html==2){ 
       $('div.saving').hide(); 
       $('div.nosuccess').fadeIn(); 
       setTimeout(function() { 
       $('div.nosuccess').fadeOut('slow'); 

       }, 3000); 
       } 




       } 
       }); 
     } 

    }); 

}); 
</script> 

,但我得到的错误是,当我按下提交按钮,我去同一个页面的查询字符串像/?email=....

它不是调用一个PHP页面,也没有隐瞒股利。

感谢

回答

2

您正在定义a变量不带引号。应该是这样的,但可能需要某种更可靠的转义:

var a  = '<?php echo $_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]; ?>'; 
+0

它工作谢谢 – air 2012-07-20 21:19:59

3

我可以建议国际海事组织一点清洁的解决方案。你的html至少是一个混乱的内联样式,jQuery可以更好,并在样式表中隔离html格式。

HTML表单:

<form id="contactform" class="t"> 
    <div class="h"> 
     <img src="images/icon-email.jpg" alt="" height="17" width="27"><span class="text-white">Email this page</span> 
    </div> 
    <div> 
     <input id="email" type="text" class="button required email"> 
    </div> 
    <div> 
     <input id="send" type="submit" value="Send" class="b"> 
    </div> 

    <div class="saving message"> <img src="validate/ajax-loader.gif"/> Please Wait Processing Request </div> 
    <div class="success message"> <img src="validate/validYes.png"/> Message Sent.<br />Thanks.</div> 
    <div class="nosuccess message"> <img src="validate/failed.png"/> Error Sending Email. </div> 
</form> 

风格:

<style type="text/css"> 
.t {width:185px; background-color:#2A677E; } 
.t div { padding:3px; }  
.b {margin-left:100px;} 
div.h {padding-top:14px;} 
#email {width:100%;} 
.message {display:none; width:175px; font-size:12px; padding:5px; text-align:center; margin-left:-25px; margin-top:0px; position:absolute;} 
.saving, .success {background-color:#D1F4DA; border:1px solid #017F1F;} 
.nosuccess {background-color:#ffebe8; border:1px solid #dd3c10;} 
</style> 

的JavaScript(jQuery的1.7+):

<script id="demo" type="text/javascript"> 
$(document).ready(function() { 

    // helper function for errorneous responses 
    var failed = function() { 
     $('div.nosuccess').fadeIn(); 
     setTimeout(function() { 
      $('div.nosuccess').fadeOut('slow'); 
     }, 3000); 
    } 

    $('div.saving').ajaxStart(function(){ 
     $('#send').attr("disabled","disabled"); 
     $(this).show(); 
    }); 

    $('div.saving').ajaxStop(function(){ 
     $(this).hide(); 
     $('#send').removeAttr("disabled"); 
    }); 

    $('#send').click(function(){ 
    //var validator = $("#contactform").validate({ 
     var $ajax = $.ajax({ 
      url: "send_page_friend.php", 
      data: { 
       "a": "http://<?php echo $_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]; ?>", 
       "email": $('#email').attr('value') 
      }, 
      type: "POST", 
      cache: false 
     }); 

     $ajax.done(function(r) { 
      if (r == '1') { 
       $('div.success').fadeIn(); 
       setTimeout(function() { 
        $('div.success').fadeOut('slow'); 
       }, 3000); 
      } else { 
       failed(); 
      } 
     }); 

     $ajax.fail(function(r) { 
      failed(); 
     }); 

     return false; 
    }); 
}); 
</script> 

一个好的做法是本地化$.ajax()并使用最新的功能.done().fail().always()作为推荐因为.success(),.error()和.complete()计划在1.8以上不推荐使用。

我不确定为什么你这样使用var validator = $("#contactform").validate({ ...,所以出于测试目的,我把$('#send').click(function(){...

快乐编码!