Im trying to create a modal popup after submitting to mailchimp. I can get the pop up to work on success but even if the form throws back a mailchimp error it stills pops up and doesn't register that it wasn't a successful send because the error came from mailchimp.
Im having trouble
Heres my script for sending the form
$('#signup').submit(function() {
$("#message").html("Submitting your entry...");
$.ajax({
url: 'inc/store-address.php',
data: $('#signup').serialize(),
success: function(msg) {
$('#message').html(msg);
$('.modal').delay(500).addClass('is-visible');
}
});
return false;
});
and here is the store-address.php file that sends the info to mailchimp
<?php
function storeAddress() {
// Validation
if(!$_GET['email']) {
return "No email address provided";
}
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
return "Email address is invalid";
}
require_once('MCAPI.class.php');
$api = new MCAPI('api-key');
$merge_vars = Array(
'EMAIL' => $_GET['email'],
'FNAME' => $_GET['fname'],
'LNAME' => $_GET['lname']
);
$list_id = "list_id";
if($api->listSubscribe($list_id, $_GET['email'], $merge_vars ) === true) {
return 'Success! ';
} else {
// An error ocurred, return error message
return '<b>Error:</b> ' . $api->errorMessage;
}
}
// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>
I will keep looking for a solution, but any help or direction much appreciated
Thanks
The request is a 200 response which is successful to the post-function, you have to check what kind of value the response actually provides to determine if it is truly a successful response. Something like this:
$.ajax({
url: 'inc/store-address.php',
data: $('#signup').serialize(),
success: function(msg) {
if(msg === 'Success! ') {
$('#message').html(msg);
$('.modal').delay(500).addClass('is-visible');
}
}
});