简体   繁体   English

从php和jquery发送电子邮件

[英]sending email from php and jquery

i am trying to send email using jquery and php. 我正在尝试使用jquery和php发送电子邮件。

my html code is 我的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>

i have following jquery code. 我有以下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>

but the error i am getting is, once i press the submit button i go same page with query string like /?email=.... 但是我得到的错误是,一旦按下提交按钮,我就会进入带有查询字符串的同一页面,如/?email=....

its not calling that php Page and also not hiding div. 它没有调用该php Page,也没有隐藏div。

Thanks 谢谢

May I suggest IMO a bit cleaner solution. 我可以建议IMO采取更清洁的解决方案。 Your html is at least a messy one with inline styles, jQuery can be better and html formatting isolated in stylesheet. 您的html至少是一个带有内联样式的乱七八糟的东西,jQuery可以更好,并且html格式可以在样式表中隔离。

html form: 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>

styles: 款式:

<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+): 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>

A good practice is to localise $.ajax() and use the latest functions .done() , .fail() and .always() as recommended on jQuery page http://api.jquery.com/jQuery.ajax/ since .success(), .error() and .complete() are planned to be deprecated in 1.8+ 一个好的做法是本地化$.ajax()并使用jQuery页面http://api.jquery.com/jQuery.ajax/上推荐的最新功能.done() .fail().always() ,因为计划在1.8+版本中弃用.success()、. error()和.complete()

I'm not sure why you use var validator = $("#contactform").validate({ ... this way, so for testing purposes I put $('#send').click(function(){... 我不确定为什么要使用var validator = $("#contactform").validate({ ...这样,因此出于测试目的,我将$('#send').click(function(){...

Happy coding! 编码愉快!

You are defining a variable without the quotes. 您正在定义a不带引号a变量。 Should be something like that but could require some kind of more trustworthy escaping: 应该是这样,但可能需要某种更可信赖的转义:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM