The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

How to use checkboxes for Lists (using PHP sample code from CC)

Highlighted
Frequent Visitor

How to use checkboxes for Lists (using PHP sample code from CC)

Hi,

 

I have been working on getting a form working using the PHP sample code from this page:

 

http://developer.constantcontact.com/libraries/sample-code.html 

Email Sign up Form - PHP

This code sample creates an email sign up form that adds individual contacts to the user's contact collection; written in PHP.

Go to GitHub

 

 

Here's my problem: I cannot figure out how to submit multiple checkboxes to Constant Contact so I can update or add multiple lists at once.

 

If I use the standard select provided in the example, I can add "multiple=yes" to the <select> tag and it will show a box for selecting multiple items. This is ok, but I would rather have checkboxes if possible.

 

I attempted to re-write the code using a checkbox like this, but I am stuck as to what to put in the "name" field. Using "list" only selects the last one, obviously.

 

I tried several methods but got an error each time, this works but it only updates the last checkbox, not all of them.

 

<?php
foreach ($lists as $list) {

     echo '<input type="checkbox" name="list" value="' . $list->id . '" />' . $list->name .'<br />';
} ?>

 

 

 

1 REPLY 1
Highlighted
Honored Contributor

Re: How to use checkboxes for Lists (using PHP sample code from CC)

Hi,

 

First off I want to apologize for taking so long to get back to you!

 

You can accomplish this by making the following code changes in the sample:

 

In the Add and in the Update contact sections:

$contact->addList($_POST['list']);

Should be changed to

foreach($_POST['list'] as $list)
    $contact->addList($list);

 

In the  code where the HTML is genrated, make the following changes:

echo '<option value="' . $list->id . '">' . $list->name . '</option>';

Should be changed to

echo '<input type="checkbox" name="list[]" value="' . $list->id . '" />' . $list->name .'<br />';

 

 

With these two changes, the lists will be handled as an array of values and allow you to use checkboxes to send multiple values. I've tested the changed code and it was working properly for me. Please let me know if you have any difficulties!

 

Sincerely,

Elijah G.
API Support Engineer
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured