Mailchimp API成功消息

时间:2014-09-05 06:20:46

标签: jquery ajax forms mailchimp

我在提交给mailchimp后试图创建一个模态弹出窗口。我可以让弹出窗口成功,但即使表单抛出mailchimp错误,它仍会弹出并且不会注册它不是一个成功的发送,因为错误来自mailchimp。

我遇到了麻烦

继承我发送表格的脚本

$('#signup').submit(function() {
    $("#message").html("Submitting your entry...");

    $.ajax({
        url: 'inc/store-address.php',
        data: $('#signup').serialize(),
        success: function(msg) {
            $('#message').html(msg);
            $('.modal').delay(500).addClass('is-visible');
        }

    });
    return false;
});

这里是store-address.php文件,它将信息发送到mailchimp

 <?php

 function storeAddress() {

    // Validation
    if(!$_GET['email']) { 
        return "No email address provided"; 
    } 

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
        return "Email address is invalid"; 
    }


    require_once('MCAPI.class.php'); 

    $api = new MCAPI('api-key');

    $merge_vars = Array( 
        'EMAIL' => $_GET['email'],
        'FNAME' => $_GET['fname'], 
        'LNAME' => $_GET['lname']
        );

    $list_id = "list_id";

    if($api->listSubscribe($list_id, $_GET['email'], $merge_vars ) === true) {
        return 'Success!&nbsp;';
    } else {
        // An error ocurred, return error message   
        return '<b>Error:</b>&nbsp; ' . $api->errorMessage;
    }

}

// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>

我将一直在寻找解决方案,但任何帮助或方向都非常赞赏

由于

1 个答案:

答案 0 :(得分:1)

请求是200响应,对于后期功能是成功的,您必须检查响应实际提供的值,以确定它是否真的是成功的响应。 像这样:

$.ajax({
    url: 'inc/store-address.php',
    data: $('#signup').serialize(),
    success: function(msg) {
        if(msg === 'Success! ') {
            $('#message').html(msg);
            $('.modal').delay(500).addClass('is-visible');
        }
    }

});