0
I’m implementing a Mailchimp API for a website. I would like to use as a condition the status obtained as response by the according server as follows codes below:
Jquery / AJAX:
$('#subscribe').on('submit', function (event) {
var values = {
"fname": $('#fname').val(),
"lname": $('#lname').val(),
"email": $('#email').val()
};
$.ajax({
type: 'POST',
url: '../includes/subscribe.php',
data: values,
success: function (resp) {
if (resp == 1) {
swal("Pronto!", "Sua mensagem foi enviada com sucesso!", "success");
document.getElementById('subscribe').reset()
} else if (resp.status == 400){
swal(
'Oops...',
'Você já está cadastrado...',
'error'
)
} else {
swal(
'Oops...',
'Algo deu errado. Tente novamente...',
'error'
)
}
}
})
});
PHP:
// Put your MailChimp API and List ID hehe
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$list_id = 'XXXXXXXXXXXXXXXXXXXX';
// Let's start by including the MailChimp API wrapper
include('../includes/MailChimp.php');
// Then call/use the class
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp($api_key);
// Submit subscriber data to MailChimp
// For parameters doc, refer to: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/
// For wrapper's doc, visit: https://github.com/drewm/mailchimp-api
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $_POST["email"],
'merge_fields' => ['FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"]],
'status' => 'subscribed',
]);
if ($MailChimp->success()) {
// Success message
echo 1;
echo json_encode($MailChimp->getLastResponse());
} else {
// Display error
echo json_encode($result);
}
?>
Any idea? I’ve tried everything...

Added return, yet I got 'Undefined' in Resp.status.
– Andre Carvalho
@André in the snippet: 'echo json_encode($result);'. The $result variable is of what type ? is an array ?
– Cleo
Cleo, You hit the solution first, just add the return type (dataType: 'json',) to work...
– Andre Carvalho