FAQs

How can I build a 'Constant Contact' sign-up form?

Written

Constant Contact is a widely email list manager. It offers several ways of adding 'sign-up' forms to your site but most of these involve using code hosted by Constant Contact. There is a developer API but it's isn't straightforward to work out how to apply it to a ChronoForm. This FAQ describes how to build a simple sign-up form in ChronoForms: 

The starting point it to download a Constant Contact Class file. There are several on the internet, I chose to use James Benson's version; the main class.cc.php file is available here on GitHub.
Download the file or copy and paste the contents using the Raw button and add it to a new folder in your ChronoForms installation. I used /components/com_chronoforms/extras/constant_contact/
For the form you will need to create a standard ChronoForms to collect the user information. I added three text inputs: first_name, last_name and email. Change the green highlighted 
In the form On Submit event add a Custom code action. Open the action configuration and add this code:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

// include the CC class file
// this is file is from https://github.com/integrationservices/Constant-Contact-PHP-Developers-Code
include JPATH_SITE.DS.'components'.DS.'com_chronoforms'.DS.'extras'.DS.'constant_contact'.DS.$form->form_details->name.DS.'class.cc.php';

$cc = new cc( '##cc_username##', '##cc_password##' );
$contact_list = 1;

$email = $form->data['email'];
// add fileds to this array in the form
// Constant Contact name => Chronoform name
$form_fields = array(
  'FirstName' => 'first_name',
  'LastName'  => 'last_name',
);

$extra_fields = array();
foreach ( $form_fields as $k => $v ) {
  $extra_fields[$k] = $form->data[$v];spa
}

// check if the contact exists
$contact = $cc->query_contacts( $email );

// uncomment this line if the user makes the action themselves
$cc->set_action_type( 'contact' );

$message = '';
if ( $contact ) {
  // update the contact
  $status = $cc->update_contact( $contact['id'], $email, $contact_list, $extra_fields );

  if ( $status ) {
    $message = "Contact {$contact['id']} has been updated";
  } else {
    // if an error occurs we can debug it any various ways

    // show a simple error to the user
    $message = "Update Contact Operation failed: " . $cc->http_get_response_code_error( $cc->http_response_code );
    /*
    // or output the last http request and response strings, for debugging only
    $cc->show_last_connection();
    */
    /*
    // usually the http_response_body will contain a more descriptive error to help debug
    if ( $cc->http_response_body ) {
      $message = '<p>' . $cc->http_response_body . '</p>';
    }
    */
  }
} else {
  // create the contact
  $new_id = $cc->create_contact( $email, $contact_list, $extra_fields );

  if ( $new_id ) {
    $message .= "Contact created with ID {$new_id}";
  } else {
    // if an error occurs we can debug it any various ways

    // show a simple error to the user
    $message .= "Create Contact Operation failed: ".$cc->http_get_response_code_error( $cc->http_response_code );
    /*
    // or output the last http request and response strings, for debugging only
    $cc->show_last_connection();
    */
    /*
    // usually the http_response_body will contain a more descriptive error to help debug
    if ( $cc->http_response_body ) {
      $message .= '<p>' . $cc->http_response_body . '</p>';
    }
    */
  }
}
if ( $message ) {
  $mainframe->enqueuemessage($message);
}
?>
Replace the blue high-lighted text with your Constant Contact username and password and the id of the List that you want to subscribe users to.
Test that the form now connects to Constant Contact and adds or updates the records correctly. There are several more detailed debugging outputs in the code that are currently commented out, uncomment these if you need them.
Note that the code check to see if the email is already listed, if it is then it updates the existing record with any changes, if not then it adds a new record.
If you need to add more inputs then you can add the Constant Contact field name and matching input name from your form to the $form_fields array.