Reputation: 55
Ok. I'm using a script from Aaron Walter to connect to MailChimps API.
There are three spots on the website and I'd like to "track" where the signup came from. I'm able to get this to work with the following code but can't wrap my mind around how to adjust this code so that there are 3 different possibilities.
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');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('myAPIkey');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "myLISTid";
//this line will allow me to see "headerBox" under sign up method within
// mailchimp so I know what form was used to sign up
$merge_vars = array('signup' =>'headerBox');
if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
// It worked!
return 'Success! Check your email (and spam folder!) for your guide.';
}
else
{
// An error ocurred, return error message
return 'Error: ' . $api->errorMessage;
}
}
The problem I'm facing involves this line:
$merge_vars = array('signup' =>'headerBox');
"signup"
will have three different values, depending on which spot they accessed the form from. I hope I've been clear enough.
EDIT: From the code above, why wouldn't something like this work? Put a hidden input field for each of the forms and check to see what value is given...
if($_GET['signupMethod']=='headerBox') {
$merge_vars = array('signup' =>'headerBox');
}
if($_GET['signupMethod']=='popUp') {
$merge_vars = array('signup' =>'popUp');
}
if($_GET['signupMethod']=='footer') {
$merge_vars = array('signup' =>'footer');
}
//continue with code
if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
// It worked!
return 'Success! Check your email (and spam folder!) for your guide.';
}
else
{
// An error ocurred, return error message
return 'Error: ' . $api->errorMessage;
}
Upvotes: 0
Views: 89
Reputation: 72269
As you stated that your form method is GET
and you created a hidden field with name signupMethod
, do like below:-
<?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');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('myAPIkey');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "myLISTid";
$merge_vars = ''; //define variable first and then assign values in next lines by checking it's value
if(isset($_GET['signupMethod']) && $_GET['signupMethod']=='headerBox') {
$merge_vars = array('signup' =>'headerBox');
}
if(isset($_GET['signupMethod']) && $_GET['signupMethod']=='popUp') {
$merge_vars = array('signup' =>'popUp');
}
if(isset($_GET['signupMethod']) && $_GET['signupMethod']=='footer') {
$merge_vars = array('signup' =>'footer');
}
if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
// It worked!
return 'Success! Check your email (and spam folder!) for your guide.';
}
else
{
// An error ocurred, return error message
return 'Error: ' . $api->errorMessage;
}
?>
Upvotes: 1