Display webpage instead of...

User 1991012 Photo


Registered User
55 posts

Now when I looked at your sample I'm in position to define what I need in a more concrete way:

Option 1: When the email field gets filled, the check whether this email exists in the connected Mailchimp list is done in the background. If it exists a custom message (Email already exists!) should displayed. If it doesn't exist, nothing happens.

Option 2: When the form submit button is clicked, the check whether the email already exists in the connected Mailchimp list is done. If it exists a custom message (Email already exists!) should displayed and the submit should be cancelled (avoiding Mailchimp to display their standard page in these situations). If it doesn't exist, submit moves on forward.

Is this workable in connection with Mailchimp?
User 187934 Photo


Senior Advisor
20,239 posts

Ok I got it.
1. Add a text element to the right of the email label for your email input and give it an ID of emailInfo. Set the font size and any padding or margin. Also remove any of the default text that's automatically added.
2. Give your submit button and ID of fb-submit-button.
3. Your email input name should be email.
4. Paste the code below to your form in an html element.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
var jQ = $.noConflict(true);
/// version 1
jQ(document).ready(function() {
jQ('#fb-submit-button').prop('disabled', true);

var email = jQ("input[name='email']"), emailInfo = jQ("#emailInfo");

email.blur(function(){

var pattern = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

if( !pattern.test(email.val())) {
emailInfo.html("<font color='red'>Invalid Email</font>");
jQ('#fb-submit-button').prop('disabled', true);
}else{

jQ.ajax({
type: "POST",
data: "email="+jQ(email).val(),
url: "php/check-mailchimp-emailaddress.php",
beforeSend: function(){

emailInfo.html("<font color='blue'>Checking Email...</font>");
},
success: function(data){
if(data =="0")
{
emailInfo.html("<font color='green'>Email OK</font>");
jQ('#fb-submit-button').prop('disabled', false);

}
else if(data == "1")
{
emailInfo.html("<font color='red'>Email Already Exists</font>");
jQ('#fb-submit-button').prop('disabled', true);

}
}

});
}
});

});

</script>

5. Take the code below and save it as check-mailchimp-emailaddress.php.html then add it to a folder in your resources named php. You can't import php files so you'll remove the .html after export.
Enter the info that's needed. See comments in code.
$apikey
$listid
$server
<?php
if(isset($_POST['email'])){
$email = $_POST['email'];

$apikey = 'ENTER YOUR MAILCHIMP API KEY';
$listid = 'ENTER YOUR MAILCHIMP LIST ID';
//*** Enter your mailchimp server with a dot added to the end.
//*** Mine was at the end of the API key as us20
$server = 'us20.';

$userid = md5( strtolower( $email ) );
$auth = base64_encode( 'user:'. $apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
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_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
$json = json_decode($result);
$msg = $json->{'status'};
}
if($msg == 'subscribed'){
echo '1';
}else if($msg == '404'){
echo '0';
}

?>

I confirmed this works on checking for existing email address on my Mailchimp account.
https://ericrohloff.com/coffeecup/ccfor … -of-email/
try existing email addresses.
me@you.com
you@me.com
Post back if you need more info.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 1991012 Photo


Registered User
55 posts

Hi Eric,
I had to postpone my activities, but now, when I continued, I worked things just as you described (made few small updates), and everything works, almost ideally. Thank you so much.
I have just one optimization to be made. I would prefer:
* "Checking Email..." while checking, and "Email Already Exists" .IF. 'subscribed', messages to be displayed only
* Message "Email OK" no to be displayed (button gets active).
Can you please update your code appropriately or describe the updates and I will do it.

Thanks in advance,
Z.
User 187934 Photo


Senior Advisor
20,239 posts

If you just want the Email OK message gone.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
var jQ = $.noConflict(true);
/// version 2
jQ(document).ready(function() {
jQ('#fb-submit-button').prop('disabled', true);

var email = jQ("input[name='email']"), emailInfo = jQ("#emailInfo");

email.blur(function(){

var pattern = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

if( !pattern.test(email.val())) {
emailInfo.html("<span style='color:red'>Invalid Email</span>");
jQ('#fb-submit-button').prop('disabled', true);
}else{

jQ.ajax({
type: "POST",
data: "email="+jQ(email).val(),
url: "php/check-mailchimp-emailaddress.php",
beforeSend: function(){

emailInfo.html("<span style='color:blue'>Checking Email...</span>");
},
success: function(data){
if(data =="0")
{
emailInfo.html("");
jQ('#fb-submit-button').prop('disabled', false);

}
else if(data == "1")
{
emailInfo.html("<span style='color:red'>Email Already Exists</span>");
jQ('#fb-submit-button').prop('disabled', true);

}
}

});
}
});

});

</script>
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 1991012 Photo


Registered User
55 posts

Hi Eric,
Thank you so much. Really appreciate your help.
While we are in this Mailchimp matter, may I ask for help for one more question:
I need to have the total number of my subscribers in my Mailchimp mailing list (the one connected to the form) displayed (online) on my form. How could that be worked out?
Thanks in advance,
Z.
User 1991012 Photo


Registered User
55 posts

Hi Eric once again,
Everything worked just as expected, until it suddenly today started getting in a loop while checking. If email exists, the form correctly returns the expected message, but if it doesn't it never stops Checking.
Can I send you somewhere privately the link to my test form or the FDP file.
User 1991012 Photo


Registered User
55 posts

I just found out that this loop issue happens when you try to use an archived email address.
I used archived email addresses to repeat my tests, and it obviously worked OK till few hours ago. From that moment, for some reason, Mailchimp stopped returning the expected answer (archived email address should be treated as a non-subscribed email address - OK) for some reason, while our form waits for an answer.
User 187934 Photo


Senior Advisor
20,239 posts

Updated php to handle subscriber count and archived address. . I'm wasn't sure what you wanted to do with the subscriber count to I have it showing up next to the email check message. That can easily be tweaked to appear else where.
<?php
if(isset($_POST['email'])){
$email = $_POST['email'];

$apikey = 'ENTER YOUR MAILCHIMP API KEY';
$listid = 'ENTER YOUR MAILCHIMP LIST ID';
$server = 'us20.';

$userid = md5( strtolower( $email ) );
$auth = base64_encode( 'user:'. $apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
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_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
$json = json_decode($result);
$msg = $json->{'status'};

$url='https://'.$server.'api.mailchimp.com/3.0/lists/'.$listid.'/?apikey='.$apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
$subscriber_count= $json['stats']['member_count'];

if($msg == 'subscribed'){
$email_address = 1;
}else if($msg == '404'){
$email_address = 0;
}else if($msg == ''){
$email_address = 'archived';
}

$data = array($email_address,$subscriber_count);
echo json_encode($data);
}
?>


Updated js
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
var jQ = $.noConflict(true);
/// version 3
jQ(document).ready(function() {
jQ('#fb-submit-button').prop('disabled', true);

var email = jQ("input[name='email']"), emailInfo = jQ("#emailInfo");

email.blur(function(){

var pattern = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

if( !pattern.test(email.val())) {
emailInfo.html("<span style='color:red'>Invalid Email</span>");
jQ('#fb-submit-button').prop('disabled', true);
}else{

jQ.ajax({
type: "POST",
data: "email="+jQ(email).val(),
url: "php/check-mailchimp-emailaddress.php",
beforeSend: function(){

emailInfo.html("<span style='color:blue'>Checking Email...</span>");
},
success: function(data){
var data = jQ.parseJSON(data);
var email_address = data[0];
var subscriber_count = data[1];
if(email_address =="0" || email_address == "archived")
{
emailInfo.html("");
jQ('#fb-submit-button').prop('disabled', false);

}
else if(email_address == "1")
{
emailInfo.html("<span style='color:red'>Email Already Exists</span>");
jQ('#fb-submit-button').prop('disabled', true);

}

if(subscriber_count > 0){
emailInfo.append("<span style='color:green'>You have " +subscriber_count+" subscribers</span>");
}
}

});
}
});

});

</script>
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 1991012 Photo


Registered User
55 posts

Thanks once again for your help.
Subscribers number:
I will need to display this number independently (from the form fields being filled or not) somewhere on the form whenever the form gets opened or refreshed.
User 187934 Photo


Senior Advisor
20,239 posts

I'll need to tweak the code if your looking for the subscriber number on page load. If you can share a link to your form so I'm on the same page it would help a lot.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.