Adding FNAME field to the mcapi-simple-subscribe-jquery sample

121 views
Skip to first unread message

Taz Carper

unread,
Jan 22, 2013, 12:44:51 PM1/22/13
to mailchimp-...@googlegroups.com
I'm building off this sample file and trying to add a first name field. The email form connects with my list, so I know its communicating with the Mailchimp servers fine. It's sending the email, but the first name field wont connect properly and I cant figure out why. It's as if 'FNAME' isn't the right name in the DB, but that's what it's called in my list view. What am I missing? Thanks

My form
<form id="signup" action="<?=$_SERVER['PHP_SELF']; ?>" method="get">
 <fieldset>
<span id="response">
<? require_once('inc/store-address.php'); if($_GET['submit']){ echo storeAddress(); } ?>
 </span>
<div class="formColumn">

 <label for="fname" id="fname">First Name</label>
<input type="text" name="fname" id="fname">

</div>
<div class="formColumn">

 <label for="email" id="address-label">Email Address</label>
 <input type="text" name="email" id="email" />

</div>
 <input type="image" src="img/submit.png" name="submit" value="Join" class="btn" alt="Join" />
 </fieldset>
</form>      

Store-address.php
<?php

function storeAddress(){
// Validation
if(!$_GET['email']){ return "No email address provided"; } 
   $merge_vars = array( 
'EMAIL' => $_GET['email'],
        'FNAME' => $_GET['fname']
        
    );
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('hiding my ID');
// 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 = "hiding my list id";

if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
// It worked!
return 'Success! Check your email to confirm sign up.';
}else{
// An error ocurred, return error message
return 'Error: ' . $api->errorMessage;
}
}

// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>

mailing-list.js
$(document).ready(function() {
$('#signup').submit(function() {
// update user interface
$('#response').html('Adding email address...');
// Prepare query string and send AJAX request
$.ajax({
url: 'inc/store-address.php',
data: 'ajax=true&email=' + escape($('#email').val()) + '&fname=' + escape($('#fname').val()),
success: function(msg) {
$('#response').html(msg);
}
});
return false;
});
});

jesse

unread,
Jan 22, 2013, 1:07:46 PM1/22/13
to mailchimp-...@googlegroups.com
That generally looks fine, but really isn't an API issue. If you're still having issues with the code, you should start by adding logging at each step. You could also mark the field required to force an error to be returned, but logging what you're actually passing around everywhere should suffice.


jesse

Taz Carper

unread,
Jan 22, 2013, 4:13:45 PM1/22/13
to mailchimp-...@googlegroups.com
Forgive my ignorance, but how do I log what's being passed around? 

Taz Carper

unread,
Jan 22, 2013, 4:48:34 PM1/22/13
to mailchimp-...@googlegroups.com
Figured it out. It was a simple " / " missing from my form.

 <input type="text" name="FNAME" id="FNAME" /> instead of  <input type="text" name="FNAME" id="FNAME" >

Always just one missing character! 
Reply all
Reply to author
Forward
0 new messages