【问题标题】:Mail Chimp Subscribe In Contact FormMailchimp 在联系表中订阅
【发布时间】:2012-09-29 08:21:37
【问题描述】:

我正在尝试“集成”从我的联系表单中订阅 mailchimp 通讯/列表的功能。因此,当用户单击复选框并提交订阅它们的表单时。

我已经尝试过这个教程:http://www.joshuawinn.com/subscribe-to-mailchimp-newsletter-option-on-contact-form/

这是我的 PHP 代码:

<?php
if( isset($_POST) ){

//cut//

// SUBSCRIBE TO MAILING LIST OPTION – ADD TO MAILCHIMP USING API
if ( $_POST['emailUpdates'] == ‘yes’ )
{
// Include Mailchimp API class
require_once(‘http://22twenty.com/wordpress/wp-content/themes/~~~~HIDDEN/MCAPI.class.php’);

// Your API Key: http://admin.mailchimp.com/account/api/
$api = new MCAPI(‘KEYHIDDEN’);

// Your List Unique ID: http://admin.mailchimp.com/lists/ (Click “settings”)
$list_id = “IDHIDDEN”;

// Variables in your form that match up to variables on your subscriber
// list. You might have only a single ‘name’ field, no fields at all, or more
// fields that you want to sync up.
$merge_vars = array(
‘FNAME’ => $_POST['name'],
);

// SUBSCRIBE TO LIST
if ( $api->listSubscribe($list_id, $_POST['email'], $merge_vars) === true ){
$mailchimp_result = ‘Success! Check your email to confirm sign up.’;
} else {
$mailchimp_result = ‘Error: ‘ . $api->errorMessage;
}
}

希望有人可以提供帮助 杰夫

【问题讨论】:

  • 只是为了澄清它需要在一个复选框的联系表单中。

标签: php forms checkbox newsletter mailchimp


【解决方案1】:

修好了!

方法如下,`

//form validation vars
$formok = true;
$errors = array();

//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name']; 
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = $_POST['message'];
$apikey = 'key';
$listID = 'id';


    //
    if (!empty($_POST['newsletter'])) {
        $url = sprintf('http://api.mailchimp.com/1.2/?method=listSubscribe&apikey=%s&id=%s&email_address=%s&merge_vars[OPTINIP]=%s&merge_vars[NAME]=name&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR']);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        $arr = json_decode($data, true);

}
//validate form data

//validate name is not empty
if(empty($name)){
    $formok = false;
    $errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
}

//validate message is not empty
if(empty($message)){
    $formok = false;
    $errors[] = "You have not entered a message";
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
    $formok = false;
    $errors[] = "Your message must be greater than 20 characters";
}

//send email if all is ok
if($formok){
    $headers = "From: email.ca" . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p>
                  <p><strong>Name: </strong> {$name} </p>
                  <p><strong>Email Address: </strong> {$email} </p>
                  <p><strong>Telephone: </strong> {$telephone} </p>
                  <p><strong>Message: </strong> {$message} </p>
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

    mail("email@gmail.com","New Enquiry",$emailbody,$headers);

}

//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'name' => $name,
        'email' => $email,
        'telephone' => $telephone,
        'message' => $message
    ),
    'form_ok' => $formok,
    'errors' => $errors
);




//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
    //set session variables
    session_start();
    $_SESSION['cf_returndata'] = $returndata;

    //redirect back to form
    header('location: ' . $_SERVER['HTTP_REFERER']);
}

}`

我稍微改了一下,但现在效果很好!

【讨论】:

    【解决方案2】:

    试试这个:

    <link href="http://cdn-images.mailchimp.com/embedcode/slim-081711.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
        /* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
           We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
    </style>
    <div id="mc_embed_signup">
    <form action="your-action-url" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank">
        <label for="mce-EMAIL">Subscribe to our mailing list</label>
        <input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required>
        <div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
    </form>
    </div>
    

    【讨论】:

    • 我正在尝试在联系表单中执行此操作,因此当用户单击复选框然后提交时,他们就会被订阅。
    猜你喜欢
    • 2016-05-30
    • 2014-04-24
    • 1970-01-01
    • 2013-11-09
    • 2015-08-13
    • 2015-06-02
    • 2016-02-17
    • 2014-02-19
    • 2016-03-26
    相关资源
    最近更新 更多