Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 活动监视器Ajax表单提交_Javascript_Ajax_Campaign Monitor - Fatal编程技术网

Javascript 活动监视器Ajax表单提交

Javascript 活动监视器Ajax表单提交,javascript,ajax,campaign-monitor,Javascript,Ajax,Campaign Monitor,CampaignMonitor似乎已经更新了他们的代码片段,以使用不同的方式提交表单。现在在标记中有一个数据id属性。使用Ajax提交表单的常用方法都不再有效了。有人知道如何使用Ajax提交新样式的活动监视表单吗 下面是Campaign Monitor给我的代码片段: <form id="subForm" class="js-cm-form" action="https://www.createsend.com/t/subscribeerror?description=" method="

CampaignMonitor似乎已经更新了他们的代码片段,以使用不同的方式提交表单。现在在
标记中有一个
数据id
属性。使用Ajax提交表单的常用方法都不再有效了。有人知道如何使用Ajax提交新样式的活动监视表单吗

下面是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>


电子邮件

订阅


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

我正在使用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,我已经编辑了我的答案以包含它。