Javascript MailChimp订阅PHP类表单合并字段

Javascript MailChimp订阅PHP类表单合并字段,javascript,php,mailchimp,mailchimp-api-v3.0,Javascript,Php,Mailchimp,Mailchimp Api V3.0,大家好,我正在使用Tatwerat团队的MailChimp订阅PHP类表单 () 我正在创建一个html表单,它使用PHP和其他一些方法在邮件黑猩猩列表上创建新订户。 当它只有FNAME和EMAIL时,它就可以正常工作了。但是,一旦我添加了额外的字段,验证就会中断,我不知道为什么 表格: <?php $config = array( #Mailchimp details "Mailchimp_ApiKey" => "API ID REMOVED FOR POST",

大家好,我正在使用Tatwerat团队的MailChimp订阅PHP类表单 ()

我正在创建一个html表单,它使用PHP和其他一些方法在邮件黑猩猩列表上创建新订户。
当它只有FNAME和EMAIL时,它就可以正常工作了。但是,一旦我添加了额外的字段,验证就会中断,我不知道为什么

表格:

<?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>

选择网络类型
板球拦网
高尔夫球拍网
足球拦网
现在免费下载
$(“#mc嵌入式订阅表单”).validate({
规则:{
字段:{
要求:正确,
电子邮件:是的,
}
}
});
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;
    }
}

我今天设法解决了这个问题,请参见下文

新的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;
    }
}

}

选择网络类型
板球拦网
高尔夫球拍网
足球拦网
现在免费下载
$(“#mc嵌入式订阅表单”).validate({
规则:{
字段:{
要求:正确,
电子邮件:是的,
}
}
}); 

检查您订阅的公共函数($fname、$lname、$email、$pcode、$tnumber、$ntype),从itindentation中删除逗号并设置一些段落
<!-- 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>