gpt4 book ai didi

javascript - Campaign Monitor Ajax 表单提交

转载 作者:行者123 更新时间:2023-11-29 20:51:41 26 4
gpt4 key购买 nike

Campaign Monitor 似乎更新了他们的代码片段以使用不同的方法提交表单。现在在 <form>标记有一个 data-id属性。使用 Ajax 提交表单的常用方法都不再起作用了。有人知道如何使用 Ajax 提交新样式的 Campaign Monitor 表单吗?

这是 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,所以我从 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;
}

关于javascript - Campaign Monitor Ajax 表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51528519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com