Add Subscribers to iContact with MODX FormIt

Nov 19, 2014

Adding contacts to an iContact subscriber list is easy with MODX and the FormIt form processor Extra.

Login to your iContact dashboard, and under "Contacts" navigate to "Sign-up Forms"

iContact Sign Up Forms

Click "Create HTML Form", choose some colors, select the form fields you want to capture, and save the form. Then back in your Sign-up Forms dashboard, you should see your new form in the list:

iContact Sign Up Forms

Click the "View HTML" link for your new form. Scroll down to the "Manual Sign-up Form" area, copy the HTML, and paste it into your MODX Template or Chunk where you want the form rendered.

iContact Sign Up Forms

NOTE: at this point, you could just render the form on your page. However you'd be limited to the redirect URL, as far as what happens after submission. If you want to fancy up the interaction a bit, either with AJAX, or some custom FormIt validation and/or error messaging, then read on...

Use the cut command to remove the value of the action attribute of the form element, and replace it with the URL of the current MODX Resource, or your AJAX Resource, kinda like this: [[~[[*id]]]]

Paste the iContact URL into a FormIt snippet call, which should look something like this:

[[!FormIt? &hooks=`iContactPost` &validate=`fields_email:email:required` &url=``]]

Obviously you'll want to customize the property set, adding validators where required, etc. Notice we're using a custom hook. Here's a gist with the code for that iContact hook:

I won't break it all down here as the comments describe most of what's going on, but you're essentially making a cURL request to the iContact form URL, with the form field values as POST data. Take note of the comment re: the response from iContact. It would be helpful to your users if you did something with the response. The hook sets a placeholder with the response body. Alternatively, iContact has an API you can use. That would be a better solution, really. But this is an option, and MODX is all about options right? :P

For further reading, here's the Official Documentation for FormIt