ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

is this a parse order issue?

May 03, 2012 2:21pm

Subscribe [1]
  • #1 / May 03, 2012 2:21pm

    Steven Grant

    894 posts

    I’m building a MailChimp subscription form onto a site.

    So I have a standard page template

    
    
    

    that contains the snippet {sn_newsletter} that contains a simple form that submits to this template

    <?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($_SERVER['DOCUMENT_ROOT'] . '/mc/MCAPI.class.php');
     // grab an API Key from <a href="http://admin.mailchimp.com/account/api/">http://admin.mailchimp.com/account/api/</a>
     $api = new MCAPI('{lv_mailchimp_api_key}');
     
     // grab your List's Unique Id by going to <a href="http://admin.mailchimp.com/lists/">http://admin.mailchimp.com/lists/</a>
     // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
     $list_id = "{lv_mailchimp_id_{freebie_1}}";
     //$list_id = "bfc533630f";
    
     if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
      // It worked! 
      return 'Woohoo! You\'re now keeping current with happenings at Greenview';
     } else { 
      // An error ocurred, return error message 
      return 'Error: ' . $api->errorMessage;
     }
     
    }
    
    // If being called via ajax, autorun the function
    if(isset($_GET['ajax'])) { echo storeAddress(); }
    
    
    
    ?>

    now if I hardcode the list id rather than call it from Low Variables, the form submits, if I use the Low Variable I get

    Error: Invalid MailChimp List ID: bfc533630f

    Any ideas?

  • #2 / May 03, 2012 5:42pm

    Steven Grant

    894 posts

    Got a good way around this. Added the MailChimp list id into the signup form as a hidden form field, getting the value from Low Variables.

    I made an amendment to my JS to add the list id to the data string to be sent to the MailChimp API.

    Then in my StoreAddress function, I’m doing

    <?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($_SERVER['DOCUMENT_ROOT'] . '/mc/MCAPI.class.php');
     // grab an API Key from <a href="http://admin.mailchimp.com/account/api/">http://admin.mailchimp.com/account/api/</a>
     $api = new MCAPI('{lv_mailchimp_api_key}');
     
     // grab your List's Unique Id by going to <a href="http://admin.mailchimp.com/lists/">http://admin.mailchimp.com/lists/</a>
     // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
     $list_id = $_GET["mc_list_id"];
    
     if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
      // It worked! 
      return '{lv_mail_subscribed_{freebie_1}}';
     } else { 
      // An error ocurred, return error message 
      return 'Error: ' . $api->errorMessage;
     }
     
    }
    
    // If being called via ajax, autorun the function
    if(isset($_GET['ajax'])) { echo storeAddress(); }
    
    
    
    ?>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases