When I click on the button the data are sent, no problem with that but I don't see the success message and when I look the console, it write this error.
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data mailchimp_v3.js:100:16
The error is in relation with this line on the javascript
data = jQuery.parseJSON(response);
How to resolve this error and to have the message success.
Thank you.
the form to send information
<?php echo HTML::form('mailchimp', OSCOM::link('ext/api/mailchimp_v3/subscribe.php'), 'post', 'novalidate data-mailchimp="' . $mailchimp_list_anonymous . '" data-redirect="'. OSCOM::link('index.php') .'"'); ?>
<div class="col-md-12">
<?php echo HTML::inputField('email', '', 'required id="email" placeholder="' . OSCOM::getDef('entry_email_address') . '" email data-validation-email-message="' . OSCOM::getDef('enter_valid_email_address') . '" required data-validation-required-message="' . OSCOM::getDef('enter_email_address') . '""', 'email'); ?>
</div>
<div class="col-md-12">
<?php echo HTML::button(OSCOM::getDef('button_submit'), 'fa fa-send', null, 'info',null,null); ?>
</div>
<?php
echo '</form>';
?>
the script
$(document).ready(function(){
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
function validateField(element,name,value){
var required = element.attr('required');
var email = element.attr('email');
if(typeof required !== typeof undefined && required !== false){
if(value.length == 0){
element.addClass('error');
element.after('<span class="field-error">' + element.attr('data-validation-required-message') + '</span>')
return false;
}
}
if(typeof email !== typeof undefined && email !== false){
if(!re.test(value)){
valid = false;
element.addClass('error');
element.after('<span class="field-error">' + element.attr('data-validation-email-message') + '</span>')
return false;
}
}
return true;
}
$('form[data-mailchimp]').each(function(){
var form = $(this);
var list = form.attr('data-mailchimp');
var url = form.attr('action');
var redirect = form.attr('data-redirect');
function success(){
if(typeof redirect !== typeof undefined && redirect !== false){
window.location = redirect;
}
}
if(!list) return;
form.submit(function(e) {
e.preventDefault();
//Reset errors and such
form.removeClass('loading');
form.find('.field-error').remove();
form.find('.form-error').html('');
var fields = form.serializeArray();
var data = {
list : list,
email : "",
timestamp_signup : (new Date).getTime(),
merge_fields : {},
interests : {}
}
//Validate all of the fields
for(var i = 0; i < fields.length; i++){
var field = fields[i];
if(!validateField(form.find('[name='+field.name+']'),field.name,field.value)) return;
if(field.name.indexOf('email') > -1){
data.email = field.value;
}
}
//All good
form.addClass('loading');
if(data.email == ""){
form.find('.form-error').html('Please provide a valid email address.');
return;
}
$.post(url, data, function(response) {
data = jQuery.parseJSON(response);
form.removeClass('loading');
//Member already exists
if(data.status != null && data.status == 400 && data.title == "Member Exists"){
form.addClass('success');
success();
console.log('member exists already');
}
else if(data.id && data.id.length > 0){
form.addClass('success');
success();
console.log('member added');
}
else{
form.find('.form-error').html('There was an error submitting the form.');
}
});
});
});
});
More information
<anonyme> http://...../mailchimp_v3.js:100:16
r.Callbacks/i https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:27978
r.Callbacks/j.fireWith https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:28749
A https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:4:14201
.send/c/< https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:4:16491
Foolow to comment data = response;
seems correct, and I have no error, but I don't see the message : success