我正在尝试使用 ajax 以类似的方式实现此解决方案,但我得到“bool:false”作为结果var_dump
。
代码中是否有任何错误?我已经工作了几个小时,但也许我错过了一些东西。请一些帮助会很棒!
订阅表格:
<form action="scripts/subscribe.php" method="post" id="subscribe_form" novalidate="novalidate">
<div class="input-group">
<input class="form-control" type="email" name="email" id="subscribe_email" placeholder="¿Cuál es tu correo electrónico?">
<div class="input-group-btn">
<button type="submit" id="subscribe_submit" data-loading-text="•••"><i class="icon-envelope"></i></button>
</div>
</div>
</form>
自定义 JS
$('#subscribe_form').submit(function() {
$('.error').hide();
$('.error').fadeIn();
// submit the form
if($(this).valid()){
$('#subscribe_submit').button('loading');
var action = $(this).attr('action');
$.ajax({
url: action,
type: 'POST',
data: {
newsletter_email: $('#subscribe_email').val()
},
success: function(data) {
$('#subscribe_submit').button('reset');
//Use modal popups to display messages
$('#modalMessage .modal-title').html('<i class="icon icon-envelope-open"></i>' + data);
$('#modalMessage').modal('show');
},
error: function() {
$('#subscribe_submit').button('reset');
//Use modal popups to display messages
$('#modalMessage .modal-title').html('<i class="icon icon-ban"></i>Oops!<br>Something went wrong!');
$('#modalMessage').modal('show');
}
});
}
return false;
});
Mailchimp 脚本:
$apikey = '<Apikey>';
$auth = base64_encode( 'user:'.$apikey );
$email = $_POST['newsletter_email'];
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'pending'
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://us11.api.mailchimp.com/3.0/lists/<ListID>/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
var_dump($result);
die('Mailchimp executed');