How do I add/update subscribers to more than one list through a form?

Britt Malka

Member
Thanks to the script you gave me, I can now add new subscribers and update existing subscribers to a list.

I use the following form code to add them one list and to add a custom field:

Code:
<form accept-charset="utf-8" action="http://DOMAIN.com/custom/list-subscribe/" method="post" target="_blank">
<input name="list" type="hidden" value="ta5238pvhl913" />
<input name="FNAME" type="text" value="" placeholder="First name" />
<input name="LNAME" type="text" value="" placeholder="Last name" />
<input name="EMAIL" required="" type="text" value="" placeholder="email" />
<input name="CODE" type="HIDDEN" value="CODE" />
<button type="submit">Sign up here</button></form>

I need to add them to more than one list. Sometimes two or three. I've tried making a copy of the "list" input and used the value for the second list, and I've tried adding the second list to the existing input field, separated by a comma. But neither worked.

Can it be done? Or should I set up a weekly export/import routine?
 
You need a bit more than this, the input must be transformed like:
Code:
<input name="list[]" type="hidden" value="ta5238pvhl913" />
Notice that now is list[] not list.

And then the code has to change slightly as follows:
on line 9 where it says:
Code:
$list = isset($_POST['list']) && strlen($_POST['list']) == 13 ? $_POST['list'] : '';
Make it:
Code:
$list = isset($_POST['list']) && is_array($_POST['list']) ? $_POST['list'] : [];

Then line 35 - 50 is:
PHP:
$endpoint = new MailWizzApi_Endpoint_ListSubscribers();
$response = $endpoint->emailSearch($list, $_POST['EMAIL']);
$bodyData = $response->body->itemAt('data');

// subscriber not found.
$url = '';
if ($response->isError && $response->httpCode == 404) {
    $response = $endpoint->create($list, $_POST);
    $bodyData = $response->body->itemAt('data');
    $url = $domainWhereMailwizzIsInstalled . '/index.php/lists/' . $list . '/update-profile/' . $bodyData['subscriber_uid'];
} elseif (!empty($bodyData['subscriber_uid'])) {
    $endpoint->update($list, $bodyData['subscriber_uid'], $_POST);
    $url = $domainWhereMailwizzIsInstalled . '/index.php/lists/' . $list . '/update-profile/' . $bodyData['subscriber_uid'];
} else {
    exit('Invalid request!');
}

Make it:
PHP:
$url      = '/';
$endpoint = new MailWizzApi_Endpoint_ListSubscribers();
foreach ($list as $l) {
    $response = $endpoint->emailSearch($l, $_POST['EMAIL']);
    $bodyData = $response->body->itemAt('data');

    // subscriber not found.
    if ($response->isError && $response->httpCode == 404) {
        $response = $endpoint->create($l, $_POST);
        $bodyData = $response->body->itemAt('data');
        $url = $domainWhereMailwizzIsInstalled . '/index.php/lists/' . $l . '/update-profile/' . $bodyData['subscriber_uid'];
    } elseif (!empty($bodyData['subscriber_uid'])) {
        $endpoint->update($l, $bodyData['subscriber_uid'], $_POST);
        $url = $domainWhereMailwizzIsInstalled . '/index.php/lists/' . $l . '/update-profile/' . $bodyData['subscriber_uid'];
    }
}

And that should do it :D
 
You're a brilliant genius :) I love MailWizz.

I had to make one correction, though, based on what you wrote to me earlier:

This is what I have in line 41 to 50:

Code:
    // subscriber not found.
    if ($response->isError && $response->httpCode == 404) {
        $response = $endpoint->create($l, $_POST);
        $bodyData = $response->body->itemAt('data');
        $url = $domainWhereMailwizzIsInstalled . '/index.php/lists/' . $l . '/update-profile/' . $bodyData['record']['subscriber_uid'];
    } elseif (!empty($bodyData['subscriber_uid'])) {
        $endpoint->update($l, $bodyData['subscriber_uid'], $_POST);
        $url = $domainWhereMailwizzIsInstalled . '/index.php/lists/' . $l . '/update-profile/' . $bodyData['subscriber_uid'];
    }
}

Do you agree? I've tested with an existing subscriber (overall) and a new one.

====
Completely off topic, I love how you can use an autoresponder sequence for a segment of your list. This is worth gold to me!
 
Back
Top