简体   繁体   English

MailChimp订阅PHP类表单合并字段

[英]MailChimp Subscribe PHP Class Form Merge Fields

Hi There I am using MailChimp Subscribe PHP Class Form by Tatwerat-Team ( link ) 嗨,我在用MailChimp由Tatwerat-Team订阅PHP类表格( link

I am creatting a html form that uses PHP and a few others to create new subscribers on a mail chimp list. 我正在创建一个使用PHP和其他一些方法的html表单,以便在黑猩猩列表上创建新的订阅者。
It works fine when its just FNAME and EMAIL however as soon as I add in additional fields that validation breaks and Im not sure why? 当仅输入FNAME和EMAIL时,它就可以正常工作,但是,一旦我添加了其他字段,验证就会中断,我不确定为什么吗?

The Form: 表格:

<?php
$config = array(
    #Mailchimp details
    "Mailchimp_ApiKey" => "API ID REMOVED FOR POST",
    "Mailchimp_ListID" => "LIST ID REMOVED FOR POST",
);
if ($_POST) {
    // First Name and Last Name Required
    require 'includes/MailChimp-Class.php';
    $MailChimp = new MailChimp($config['Mailchimp_ApiKey'], $config['Mailchimp_ListID'], TRUE);
    $MailChimp->subscribed($_POST['fname'], NULL, $_POST['email'], $_POST['pcode'], $_POST['tnumber'], $_POST['ntype']);
    echo '<div class="well">' . $MailChimp->message() . '</div>';
    $output = $MailChimp->message();
    if ($output == 'Thank you for subscribe our newsletter') {
        echo '';
    } else {
        echo 'There has been a problem. Please try again later';
    }
}
?>
<form class="subscribe" method="post" id="mc-embedded-subscribe-form">
    <div class="field">
        <input type="text" name="fname"  placeholder="Name" required>
    </div>
    <div class="field">
        <input type="email" name="email"  placeholder="Email" required>    
    </div>
    <div class="field">
        <input type="text" name="pcode" placeholder="Postcode" required>   
    </div>
    <div class="field">
        <input type="text" name="tnumber" placeholder="Telephone Number" required>  
    </div>
    <div class="field">
        <select id="ddl2" name="ntype" required>
            <option value="" disabled selected>Choose netting type</option>
            <option value="cricket">Cricket Ballstop Netting</option>
            <option value="golf">Golf Ballstop Netting</option>
            <option value="football">Football Ballstop Netting</option>
        </select>  
    </div>
    <div class="field">
        <button type="submit" title="Submit" class="button"><span> <i class="fa fa-arrow-right"></i> Download Now for FREE</span></button>
    </div>
</form>
<!--End mc_embed_signup-->
<script>
    $("#mc-embedded-subscribe-form").validate({
        rules: {
            field: {
                required: true,
                email: true,
            }
        }
    });
</script>

The Mailchimp Class PHP Mailchimp类PHP

<?php

error_reporting(E_ALL);

/*
 * 
 * MailChimp Subscribe PHP Class Form 
 * 
 * Let public visitors to subscribe your newsletter 
 * 
 * PHP Version 5.x
 *
 * Author Tatwerat-Team 
 * 
 * Author-Account http://themeforest.net/user/tatwerat-team 
 * 
 * Version 1.0
 *
 */

class MailChimp {

public $Key;
public $ListID;
public $Error;
public $Email;
public $FName;
public $LName;
public $Status = 'subscribed';
public $FullData;

public function __construct($API_Key, $List_ID, $Full_Data = TRUE) {
    $this->Key = $API_Key;
    $this->ListID = $List_ID;
    $this->FullData = $Full_Data;
}

public function subscribed($fname, $lname, $email, $pcode, $tnumber, $ntype,) {
    $this->FName = $fname;
    $this->LName = $lname;
    $this->Email = $email;
    $this->PCode = $pcode;
    $this->TNumber = $tnumber;
    $this->NType = $ntype;
    $this->message();
    if (!$this->Error)
        $this->curlData($this->apiUrl(), $this->Key, $this->jsonData(), 'PUT');
}

public function apiUrl() {
    $apiKey = $this->Key;
    $listId = $this->ListID;
    $memberId = $memberId = md5(strtolower($this->Email));
    $getapi = substr($this->escape($apiKey), strpos($this->escape($apiKey), '-') + 1);
    return 'https://' . $this->escape($getapi) . '.api.mailchimp.com/3.0/lists/' . $this->escape($listId) . '/members/' . $this->escape($memberId);
}

public function jsonData() {
    return json_encode([
        'email_address' => $this->escape($this->Email),
        'status' => $this->escape($this->Status),
        'merge_fields' => [
            'NAME' => $this->escape($this->FName),
            'PCODE' => $this->escape($this->PCode),
            'TNUMBER' => $this->escape($this->TNumber),
            'NTYPE' => $this->escape($this->NType),
        ]
    ]);
}

public function curlData($url, $apiKey, $json, $type, $get = FALSE) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($get)
        echo var_dump(json_decode($result));
    else
        $this->Error = ($httpCode != 200) ? "[error-" . $httpCode . "]" : '';
}

public function validate() {
    if (empty($this->FName) and $this->FullData) {
        $this->Error = 'First name required';
    } elseif (empty($this->PCode)) {
        $this->Error = 'Email required';
    } elseif (empty($this->TNumber)) {
        $this->Error = 'Email required';
    } elseif (empty($this->NType)) {
        $this->Error = 'Email required';
    } elseif (empty($this->Email)) {
        $this->Error = 'Email required';
    } elseif (!filter_var($this->Email, FILTER_VALIDATE_EMAIL)) {
        $this->Error = 'Invalid your email';
    }
}

public function escape($string) {
    return htmlspecialchars(trim($string), ENT_QUOTES, 'UTF-8');
}

public function message() {
    $this->validate();
    if (!$this->Error) {
        return 'Thank you for subscribe our newsletter';
    } else {
        if ($this->Error == "[error-0]") {
            $this->Error = 'Bad request';
            return;
        } elseif ($this->Error == "[error-400]") {
            $this->Error = 'Invalid your email';
            return;
        } elseif ($this->Error == "[error-401]") {
            $this->Error = 'Invalid API key';
            return;
        } elseif ($this->Error == "[error-404]") {
            $this->Error = 'Invalid list ID';
            return;
        }
        return $this->Error;
    }
}

} }

I managed to work this out today, please see below. 我今天设法解决了这个问题,请参阅下文。

New MailChimp_Class.php File 新的MailChimp_Class.php文件

<?php

error_reporting(E_ALL);

/*
 * 
 * MailChimp Subscribe PHP Class Form 
 * 
 * Let public visitors to subscribe your newsletter 
 * 
 * PHP Version 5.x
 *
 * Author Tatwerat-Team 
 * 
 * Author-Account http://themeforest.net/user/tatwerat-team 
 * 
 * Version 1.0
 *
 */

class MailChimp {

public $Key;
public $ListID;
public $Error;
public $Email;
public $FName;
public $LName;
public $PCode;
public $TNumber;
public $NType;
public $Status = 'subscribed';
public $FullData;

public function __construct($API_Key, $List_ID, $Full_Data = TRUE) {
    $this->Key = $API_Key;
    $this->ListID = $List_ID;
    $this->FullData = $Full_Data;
}

public function subscribed($fname, $lname, $email, $pcode, $tnumber, $ntype) {
    $this->FName = $fname;
    $this->LName = $lname;
    $this->Email = $email;
    $this->PCode = $pcode;
    $this->TNumber = $tnumber;
    $this->NType = $ntype;
    $this->message();
    if (!$this->Error)
        $this->curlData($this->apiUrl(), $this->Key, $this->jsonData(), 'PUT');
}

public function apiUrl() {
    $apiKey = $this->Key;
    $listId = $this->ListID;
    $memberId = $memberId = md5(strtolower($this->Email));
    $getapi = substr($this->escape($apiKey), strpos($this->escape($apiKey), '-') + 1);
    return 'https://' . $this->escape($getapi) . '.api.mailchimp.com/3.0/lists/' . $this->escape($listId) . '/members/' . $this->escape($memberId);
}

public function jsonData() {
    return json_encode([
        'email_address' => $this->escape($this->Email),
        'status' => $this->escape($this->Status),
        'merge_fields' => [
            'FNAME' => $this->escape($this->FName),
            'PCODE' => $this->escape($this->PCode),
            'TNUMBER' => $this->escape($this->TNumber),
            'NTYPE' => $this->escape($this->NType),
        ]
    ]);
}

public function curlData($url, $apiKey, $json, $type, $get = FALSE) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($get)
        echo var_dump(json_decode($result));
    else
        $this->Error = ($httpCode != 200) ? "[error-" . $httpCode . "]" : '';
}

public function validate() {
    if (empty($this->FName) and $this->FullData) {
        $this->Error = 'Please Enter Your Name';
    } elseif (empty($this->Email)) {
        $this->Error = 'Please Enter Your Email';
    } elseif (!filter_var($this->Email, FILTER_VALIDATE_EMAIL)) {
        $this->Error = 'Please Enter a Valid Email';
    } elseif (empty($this->PCode) and $this->FullData) {
        $this->Error = 'Please Enter Your Postcode';
    } elseif (empty($this->TNumber) and $this->FullData) {
        $this->Error = 'Please Enter Your Telephone Number';
    } elseif (empty($this->NType) and $this->FullData) {
        $this->Error = 'Please Select a Netting Type';
    }
}

public function escape($string) {
    return htmlspecialchars(trim($string), ENT_QUOTES, 'UTF-8');
}

public function message() {
    $this->validate();
    if (!$this->Error) {
        return 'Thank you for subscribing to our newsletter';
    } else {
        if ($this->Error == "[error-0]") {
            $this->Error = 'Bad request';
            return;
        } elseif ($this->Error == "[error-400]") {
            $this->Error = 'Invalid your email';
            return;
        } elseif ($this->Error == "[error-401]") {
            $this->Error = 'Invalid API key';
            return;
        } elseif ($this->Error == "[error-404]") {
            $this->Error = 'Invalid list ID';
            return;
        }
        return $this->Error;
    }
}

}

New HTML 新的HTML

<!-- Begin MailChimp Signup Form -->

<?php
    $config = array(
        #Mailchimp details
        "Mailchimp_ApiKey" => "insert apikey",
        "Mailchimp_ListID" => "insert list id",
    );
    if ($_POST) {
        // First Name and Last Name Required
        require 'includes/MailChimp-Class.php';
        $MailChimp = new MailChimp($config['Mailchimp_ApiKey'], $config['Mailchimp_ListID'], TRUE);
        $MailChimp->subscribed($_POST['fname'], NULL, $_POST['email'], $_POST['pcode'], $_POST['tnumber'], $_POST['ntype']);
        echo '<div class="well">' . $MailChimp->message() . '</div>';

        $output = $MailChimp->message();
        if ($output == 'Thank you for subscribing to our newsletter') {
        echo '<script type="text/javascript">
                   window.top.location.href = "http://example.com"
              </script>';
        } else { echo ''; }

    }
    ?>
    <form class="subscribe" method="post" id="mc-embedded-subscribe-form">
        <div class="field">
            <input type="text" name="fname"  placeholder="Name">
        </div>
        <div class="field">
            <input type="email" name="email"  placeholder="Email">    
        </div>
        <div class="field">
            <input type="text" name="pcode" placeholder="Postcode">   
        </div>
        <div class="field">
            <input type="text" name="tnumber" placeholder="Telephone Number">  
        </div>
        <div class="field">
            <select id="ddl2" name="ntype">
                <option value="Choose netting type" disabled selected>Choose netting type</option>
                <option value="Cricket Ballstop Netting">Cricket Ballstop Netting</option>
                <option value="Golf Ballstop Netting">Golf Ballstop Netting</option>
                <option value="Football Ballstop Netting">Football Ballstop Netting</option>
            </select>  
        </div>
        <div class="field">
            <button type="submit" title="Submit" class="button"><span> <i class="fa fa-arrow-right"></i> Download Now for FREE</span></button>
        </div>
    </form>
<!--End mc_embed_signup-->

<script>
    $( "#mc-embedded-subscribe-form" ).validate({
      rules: {
        field: {
          required: true,
          email: true,
        }
      }
    }); 
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM