【问题标题】:Campaign Monitor Ajax form submissionCampaign Monitor Ajax 表单提交
【发布时间】:2018-09-05 22:51:47
【问题描述】:

Campaign Monitor 似乎已经更新了他们的代码 sn-ps 以使用不同的提交表单的方法。现在在<form> 标签中有一个data-id 属性。使用 Ajax 提交表单的常用方法都不再适用。有谁知道如何使用 Ajax 提交新样式的 Campaign Monitor 表单?

这是 sn-p Campaign Monitor 给我的代码:

<form id="subForm" class="js-cm-form" action="https://www.createsend.com/t/subscribeerror?description=" method="post" data-id="A61C50BDC994654B1D79D5719EC1255C09788D1CED7F46D508DAE8944C2CB34BA5EC78954EB81FB5F54AD0716B1F245E696D5CFAF72B819D19DC3B44517">    
<p>
    <label for="fieldEmail">Email</label>
    <br />
    <input id="fieldEmail" name="cm-wmpt-wmpt" type="email" class="js-cm-email-input"
    required />
</p>
<p>
    <button class="js-cm-submit-button" type="submit">Subscribe</button>
</p>
</form>
<script type="text/javascript" src="https://js.createsend1.com/javascript/copypastesubscribeformlogic.js"></script>

【问题讨论】:

    标签: javascript ajax campaign-monitor


    【解决方案1】:

    我向他们的支持部门发送了电子邮件,因此我可以自己回答问题。他们已经用他们的 API 替换了所有旧方法。您需要将表单的操作设置为您自己的端点(例如 signup.php)。

    我正在使用 PHP,所以我从 https://github.com/campaignmonitor/createsend-php 下载了他们的 PHP API 包装器。一个简单的例子是这样的:

    require_once 'lib/campaignmonitor/csrest_subscribers.php';
    
    $auth = array(
        'api_key' => 'Your API key'
    );
    $wrap = new CS_REST_Subscribers( 'Your list ID', $auth );
    
    $result = $wrap->add( array(
        'EmailAddress' => 'Subscriber email',
        'Name' => 'Subscriber name',
        'CustomFields' => array(
            array(
                'Key' => 'Field 1 Key',
                'Value' => 'Field Value'
            ),
            array(
                'Key' => 'Field 2 Key',
                'Value' => 'Field Value'
            ),
            array(
                'Key' => 'Multi Option Field 1',
                'Value' => 'Option 1'
            ),
            array(
                'Key' => 'Multi Option Field 1',
                'Value' => 'Option 2'
            )
        ),
        'ConsentToTrack' => 'yes',
        'Resubscribe' => true
    ) );
    

    更新:如果你好奇,这是我完整的 PHP:

    require_once 'libs/campaignmonitor/csrest_subscribers.php';
    
    $success_message = 'You\'ve been signed up for our email list.';
    $error_message_general = 'There was a problem signing you up for the email list. Please try again.';
    $error_message_format = 'Please enter a valid email address.';
    
    if ( $_SERVER[ 'REQUEST_METHOD' ] !== 'POST' ) {
        renderResponse( true, $error_message_general );
    }
    else {
    
        $api_key = 'your_api_key_here';
        $list_id = 'your_list_id_here';
    
        $email = array_key_exists( 'email', $_POST ) ? $_POST[ 'email' ] : '';
        $email = cleanInput( $email );
    
        if ( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
            try {
                $auth = array(
                    'api_key' => $api_key
                );
                $wrap = new CS_REST_Subscribers( $list_id, $auth );
    
                $result = $wrap->add( array(
                    'EmailAddress' => $email,
                    'ConsentToTrack' => 'yes',
                    'Resubscribe' => true
                ) );
    
                if ( $result->was_successful() )
                    renderResponse( false, $success_message );
                else
                    renderResponse( true, $error_message_general );
            }
            catch ( Exception $e ) {
                renderResponse( true, $error_message_general );
            }
        }
        else {
            renderResponse( true, $error_message_format );
        }
    
    }
    
    function renderResponse( $error, $message ) {
        header( 'Content-Type: application/json' );
        $result = [
            'error' => $error,
            'message' => $message
        ];
        echo json_encode( $result );
        die();
    }
    
    function cleanInput( $data ) {
        $data = trim( $data );
        $data = stripslashes( $data );
        $data = htmlspecialchars( $data );
        return $data;
    }
    

    【讨论】:

    • 我很想知道这如何与实际的定制订阅表单一起工作?请问你有这个小提琴吗?
    • 这只是一个简单的jQuery ajax 表单提交。如果您想查看我的完整 PHP,我已经编辑了我的答案以包含它。
    猜你喜欢
    • 1970-01-01
    • 2020-07-12
    • 2014-05-02
    • 2023-03-13
    • 2011-06-17
    • 2014-02-19
    • 2011-05-09
    • 1970-01-01
    相关资源
    最近更新 更多