I'm trying to post a segment to mail chimp accounts via AJAX. If the call is successful, I want to redirect to a URL. After much testing, the calls are successful every time, but I can't get a redirect...it's as if the code runs and then skips past the if "result = success", and goes right to my else statement which is just an alert. When I view the data being returned I get "nullsuccess". I've even tried if result = "nullsuccess" and it still goes right for the else statement.
$("#mc-embedded-subscribe-form").submit(function(e) {
var url = $(this).prop('action'); // the script where you handle the form input.
$.ajax({
type: "POST",
url: "update-member.php",
data: $("#mc-embedded-subscribe-form").serialize(), // serializes the form's elements.
dataType: "text",
success: function(data){
if(data.result == 'success'){
window.location.href = 'https://google.com';
}else{
alert("ERROR-ONLY IT STILL SUBMITS DATA!");
}
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
Above for dataType - I've used "text" because if I use "json" or "jsonp", it completely ignores the success function, both if and else statements. Funny thing is all three data types will still post the data successfully.
And here is the PHP(update-member.php)
$apikey = 'myAPIKEYHERE';
$auth = base64_encode( 'user:'.$apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $_POST['email_address'],
'status' => 'subscribed',
'merge_fields' => array(
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'MYMAILCHIMPURL');
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);
echo json_encode($response_array);
$result = curl_exec($ch);
$message = curl_errno($ch) === CURLE_OK ? 'success' : 'failure';
echo $message; //echos nullsuccess
I feel like I'm not returning something right from update-member.php, or I'm not gathering it properly in my ajax success function.