在同一页面上提交表单时显示ajax成功或错误消息

时间:2013-11-24 23:47:01

标签: php jquery ajax

我在DIV标签中找到了apear但不确定如何将电子邮件地址发送到外部电子邮件数据库//myguestlist.com/mgl/formreceiver.php。

这是HTML

                

成为第一个了解第七频道布里斯班赛车的人                   嘉年华:

                
              method="post" id="mf528c0a39d76be"> <input class="mailchimp-input"

                input="" name="PatronEmail" id="mf528c0a39d76be_PatronEmail"

                placeholder="Enter your email..." type="text"> <button type="submit"

                class="btn"><span class="singup-image"><img src="img/send.png"

                    alt="send email"></span><span class="singup-text">Send</span></button>
            </form>
            <div class="success-message"></div>
            <div class="error-message"></div>
          </div>

这是PHP:

<?php

//电子邮件地址验证

function isEmail($ email){     回报(的preg_match(“/ ^ [-_ [:alnum:]] + @((([[:alnum:]] | [[:alnum:]] [[:alnum:] - ] * [[:alnum :]]))+(广告| AE |航空| AF | AG | AI | AL |上午|一个| AO |水溶液| AR | ARPA |为|在| AU | AW | AZ | BA | BB | BD |。是| BF | BG | BH | BI |商业|北京| BM |亿|博| BR | BS | BT | BV | BW |由| BZ | CA | CC | CD | CF | CG | CH | CI | CK | CL |厘米|爱泰克|合作| COM |会展|点| CS |铜|简历| CX | CY | CZ |代| DJ | DK |数据|做| DZ | EC | EDU | EE |如|诶|呃| ES |等| EU |音响| FJ | FK | FM | FO | FR | GA | GB | GD | GE | GF | GH | GI | GL |克| GN | GOV | GP | GQ | GR | GS | GT |顾| GW | GY |香港| HM | HN |人力资源| HT |胡锦涛| ID |即| IL |中|资讯|国际| IO | IQ |红外|是|它| JM |乔|日本|科|公斤| KH |き|千米| KN | KP | KR |千瓦| KY | KZ | LA |磅| LC |李| LK | LR | LS | LT |陆| LV | LY |毫安| MC | MD |毫克| MH |密耳| MK |毫升|毫米| MN | MO | MP | MQ |先生|毫秒| MT |亩|博物馆| MV | MW | MX |我| MZ | NA |名称| NC | NE |净| NF | NG | NI | NL |没有| NP | NR | NT | NU |新西兰| OM |组织| PA | PE | PF | PG | pH值| PK | PL |发短消息| PN |公关|亲| PS | PT | PW | PY | QA |重新| RO | RU | RW | SA | SB | SC | SD | SE | SG | SH | SI | SJ | SK | SL | SM | SN |所以| SR | ST |苏| SV | SY |深圳| TC | TD | TF | TG |日| TJ | TK | TM | TN |于| TP | TR | TT |电视| TW |雅轩| UA | UG |英国| UM |美|乌伊| UZ | VA | VC |已经| VG | VI | VN |似曾相识| WF | WS |烨| YT |宇| ZA | ZM | ZW)$ |(([0-9] [0-9] | [0-1] [0- 9] [0-9] | [2] [0-4] [0-9] |。[2] [5] [0-5])){3}([0-9] [0-9] ?| [0-1] [0-9] [0-9] | [2] [0-4] [0-9] | [2 ] [5] [0-5]))$ / i“,$ email)); }

if($ _ POST){

// Enter the email where you want to receive the notification when someone subscribes
$emailTo = 'wbriton@brc.com.au';

$subscriber_email = ($_POST['PatronEmail']);

if(!isEmail($subscriber_Email)) {
    $array = array();
    $array['valid'] = 0;
    $array['message'] = 'Insert a valid email address!';
    echo json_encode($array);
}
else {
    $array = array();
    $array['valid'] = 1;
    $array['message'] = 'Thanks for your subscription!';
    echo json_encode($array);

    // Send email
$subject = 'New Subscriber!';
$body = "You have a new subscriber!\n\nEmail: " . $subscriber_email;
    // uncomment this to set the From and Reply-To emails, then pass the $headers variable to the "mail" function below
//$headers = "From: ".$subscriber_email." <" . $subscriber_email . ">" . "\r\n" . "Reply-To: " . $subscriber_email;
mail($emailTo, $subject, $body);
}

}

&GT;

这是Java脚本

$( '成功的消息。')隐藏();     $( '错误消息。')隐藏();

$('.subscribe form').submit(function() {
    var postdata = $('.subscribe form').serialize();
    $.ajax({
        type: 'POST',
        url: 'php/sendmail.php',
        data: postdata,
        dataType: 'json',
        success: function(json) {
            if(json.valid == 0) {
                $('.success-message').hide();
                $('.error-message').hide();
                $('.error-message').html(json.message);
                $('.error-message').fadeIn();
            }
            else {
                $('.error-message').hide();
                $('.success-message').hide();
                $('.subscribe form').hide();
                $('.success-message').html(json.message);
                $('.success-message').fadeIn();
            }
        }
    });
    return false;
});

0 个答案:

没有答案