Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72416

How can I send leads to specific mailing list depending on their selection (Using Mailchimp)?

$
0
0

I am trying to link my newsletter form with my Mailchimp list however, I want the lead to go into a specific list depending on the selection the user makes.

For example, if they select the "I am a Worker" option from the HTML code below, they will be subscribed into the "Workers" mailing list. Now, if they select the "I am a Business" option from the HTML code below, they will be subscribed into the "Business" mailing list.

My HTML code is here:

<form class="form">
                        <div class="row">
                            <div class="col-md-3 col-sm-12 px-1">
                                <div class="form-group">
                                    <input type="text" class="form-control" placeholder="First Name*">
                                </div>
                            </div>
                            <div class="col-md-4 col-sm-12 px-1">
                                <div class="form-group">
                                    <input type="email" class="form-control" placeholder="Email Address*">
                                </div>
                            </div>
                            <div class="col-md-3 col-sm-12 px-1">
                                <div class="form-group">
                                    <select class="form-control">
                                        <option selected disabled>-- Please Select --</option>
                                        <option>I am a Worker</option>
                                        <option>I am a Business</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-2 col-sm-12 px-1 submit-btn">
                                <button type="submit" class="btn btn-primary newsletter-btn">Notify Me</button>
                            </div>
                        </div>
                    </form>

Here is the current notify-me.php file I am using:

<?php

// Set to "mailchimp" or "file"
$STORE_MODE = "mailchimp";

// MailChimp API Key findable in your Mailchimp's dashboard
$API_KEY =  "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XX0";

// MailChimp List ID  findable in your Mailchimp's dashboard
$LIST_ID =  "XXXXXXXXXX";

require('MailChimp.php');

/* ***************************************************** */
// For the part below, no interventions are required
/* ***************************************************** */

if($_SERVER["REQUEST_METHOD"] == "POST"&& !empty($_POST["email"])) {

    $email = $_POST["email"];

    header('HTTP/1.1 200 OK');
    header('Status: 200 OK');
    header('Content-type: application/json');

    // Checking if the email writing is good
    if(filter_var($email, FILTER_VALIDATE_EMAIL)) {

        // The part for the storage in a .txt
        if ($STORE_MODE == "file") {

            // SUCCESS SENDING
            if(@file_put_contents($STORE_FILE, strtolower($email)."\r\n", FILE_APPEND)) {
                echo json_encode(array(
                        "status" => "success"
                    ));
            // ERROR SENDING
            } else {
                echo json_encode(array(
                        "status" => "error",
                        "type" => "FileAccessError"
                    ));
            }

        // The part for the storage in Mailchimp
        } elseif ($STORE_MODE == "mailchimp") {

            $MailChimp = new \Drewm\MailChimp($API_KEY);

            $result = $MailChimp->call('lists/subscribe', array(
                        'id'                => $LIST_ID,
                        'email'             => array('email'=>$email),
                        'double_optin'      => false,
                        'update_existing'   => true,
                        'replace_interests' => false,
                        'send_welcome'      => true,
                    ));     

            // SUCCESS SENDING
            if($result["email"] == $email) {        
                echo json_encode(array(
                        "status" => "success"
                    ));
            // ERROR SENDING
            } else {
                echo json_encode(array(
                        "status" => "error",
                        "type" => $result["name"]
                    ));
            }
        // ERROR
        } else {
            echo json_encode(array(
                    "status" => "error",
                ));
        }
    // ERROR DURING THE VALIDATION 
    } else {
        echo json_encode(array(
                "status" => "error",
                "type" => "ValidationError"
            ));
    }
} else {
    header('HTTP/1.1 403 Forbidden');
    header('Status: 403 Forbidden');
}

?>

Here is the current MailChimp.php file I am using:

<?php

namespace Drewm;

class MailChimp
{
    private $api_key;
    private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0';
    private $verify_ssl   = false;

    /**
     * Create a new instance
     * @param string $api_key Your MailChimp API key
     */
    function __construct($api_key)
    {
        $this->api_key = $api_key;
        list(, $datacentre) = explode('-', $this->api_key);
        $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
    }

    /**
     * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
     * @param  string $method The API method to call, e.g. 'lists/list'
     * @param  array  $args   An array of arguments to pass to the method. Will be json-encoded for you.
     * @return array          Associative array of json decoded API response.
     */
    public function call($method, $args=array())
    {
        return $this->makeRequest($method, $args);
    }

    /**
     * Performs the underlying HTTP request. Not very exciting
     * @param  string $method The API method to be called
     * @param  array  $args   Assoc array of parameters to be passed
     * @return array          Assoc array of decoded result
     */
    private function makeRequest($method, $args=array())
    {      
        $args['apikey'] = $this->api_key;

        $url = $this->api_endpoint.'/'.$method.'.json';

        if (function_exists('curl_init') && function_exists('curl_setopt')){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');       
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
            $result = curl_exec($ch);
            curl_close($ch);
        } else {
            $json_data = json_encode($args);
            $result    = file_get_contents($url, null, stream_context_create(array(
                'http' => array(
                    'protocol_version' => 1.1,
                    'user_agent'       => 'PHP-MCAPI/2.0',
                    'method'           => 'POST',
                    'header'           => "Content-type: application/json\r\n".
                                          "Connection: close\r\n" .
                                          "Content-length: " . strlen($json_data) . "\r\n",
                    'content'          => $json_data,
                ),
            )));
        }

        return $result ? json_decode($result, true) : false;
    }
}

Please bare in mind that my coding skills are next to non-existent. It would be very helpful and much appreciated if you could point me to a video tutorial so I can watch and learn how to do it.

Would anyone happen to know how I can make it work?

Any guidance would be appreciated, thanks in advance!


Viewing all articles
Browse latest Browse all 72416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>