Page 1 of 1
SmartForms and Mailchimp API
Posted: Mon May 20, 2024 12:25 pm
by johnboyuk1
Has anyone used these together ... and can give me any pointers?
Is there a way of processing parts of a SmartForm from so that the Mailchimp API is called and values are sent? Not sure if its possible in SmartForms ... would need to be some kind of handler in SmartForm where I can process PHP I assume, but I can't see a way of doing that...
Any guidance appreciated !
Re: SmartForms and Mailchimp API
Posted: Mon May 20, 2024 12:31 pm
by velden
IIRC you can use a handler with smarty, hence calling a UDT (or plugin) and passing parameters
Re: SmartForms and Mailchimp API
Posted: Mon May 20, 2024 12:38 pm
by magallo
Yes, you can process parts of a SmartForm in CMS Made Simple to call the Mailchimp API by using a UDT as a form handler:
1) Create a UDT:
Go to Extensions > User Defined Tags and create a new UDT. Name it send_to_mailchimp. Add the following PHP code to handle the Mailchimp API call:
Code: Select all
$api_key = 'your_mailchimp_api_key';
$list_id = 'your_list_id';
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$data_center = substr($api_key,strpos($api_key,'-')+1);
$url = 'https://' . $data_center . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/';
.........
2) Modify the SmartForm to Call the UDT:
Add a hidden field to your SmartForm to specify the UDT handler. Update your form code like this:
Code: Select all
<input type="hidden" name="m1_udt" value="send_to_mailchimp">
3) Process the Form:
Code: Select all
{if isset($_POST.m1_udt) && $_POST.m1_udt == 'send_to_mailchimp'}
{send_to_mailchimp}
{/if}
I personally like to use ajax but let me know how it goes
Re: SmartForms and Mailchimp API
Posted: Mon May 20, 2024 12:56 pm
by johnboyuk1
oooh... thanks for this! Let me try it out!
Re: SmartForms and Mailchimp API
Posted: Mon May 20, 2024 12:58 pm
by magallo
it's possible that "m1_" will need to be "cntnt01" check {$actionid}
Re: SmartForms and Mailchimp API
Posted: Mon May 20, 2024 2:52 pm
by velden
I don't understand why to use the hidden field. Could you explain it?
Re: SmartForms and Mailchimp API
Posted: Mon May 20, 2024 3:51 pm
by magallo
I guess it's not necessary if you want to trigger the UDT on every submission.
Re: SmartForms and Mailchimp API
Posted: Mon May 20, 2024 4:15 pm
by johnboyuk1
What do I need to do if not using the hidden field? I would like to trigger the UDT on every submission ... thanks!
Re: SmartForms and Mailchimp API
Posted: Tue May 21, 2024 8:16 am
by johnboyuk1
Bit stuck on this (no surprise!) ... does all this code:
Code: Select all
<input type="hidden" name="m1_udt" value="send_to_mailchimp">
{if isset($_POST.m1_udt) && $_POST.m1_udt == 'send_to_mailchimp'}
{send_to_mailchimp}
{/if}
go directly in the SmartForm design template ... for example just above where the code for the submit button is?
I tried the above but nothing seems to be happening
...grateful for any help!
Re: SmartForms and Mailchimp API
Posted: Tue May 21, 2024 9:02 am
by magallo
two options you could try:
1) inside your form template:
{smtfm_form_errors assign='errors'}
{if !empty($errors)}
<ul class="error">
{foreach $errors as $err}
<li>{$err}</li>
{/foreach}
</ul>
{/if}
{send_to_mailchimp}
<form>
2) try putting the {send_to_mailchimp} in the "Form Output" tab in the "Smarty template for the final message to display to the user" box when you edit a form
Re: SmartForms and Mailchimp API
Posted: Tue May 21, 2024 9:11 am
by velden
I think you should have a good understanding of the module to use it.
Have a look at the help, the Validation tab and the available Handlers.
Note that there's a 'POST form results to a URL/WebHook' handler, a 'Stop processing dispositions based on a condition' (it allows Smarty) and a 'Validate via Smarty' validation option.
I expect you can choose one to do what you want.
Re: SmartForms and Mailchimp API
Posted: Tue May 21, 2024 9:15 am
by magallo
"Stop processing dispositions based on a condition" seems to be also an option. It will not give you the thank you message

Re: SmartForms and Mailchimp API
Posted: Tue May 21, 2024 9:20 am
by velden
Well, if you make sure your Smarty code doesn't output anything on success, other dispositions will continue.
PLUS, as stated in the description, the final 'disposition' will be done anyway. I think that makes it possible to show the 'thank you message' regardless of the outcome of the earlier dispositions.
Re: SmartForms and Mailchimp API
Posted: Tue May 21, 2024 9:23 am
by magallo
i think the important thing is, we gave our new friend johnboyuk1 some material to work with.
Re: SmartForms and Mailchimp API
Posted: Tue May 21, 2024 9:38 am
by johnboyuk1
Yes - super helpful. I'm getting somewhere now I've got the full API code...I have something rudimentary working, I'll paste full Mailchimp API code when im done in case it's useful to anyone else.
