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


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



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

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.


foreach ($lists as $list) {

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







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:


Should be changed to

foreach($_POST['list'] as $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!



Elijah G.
API Support Engineer
Developer Portal

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

Visit Page