未加星标

MailChimp Subscribe Form with PHP, jQuery and AJAX (API 3.0)

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Original post using (older and no longer supported) MailChimp API 1.0 can be foundhere.

What this code does Validate field contents Pass the form submission to MailChimp and add the record to your list Basic fallback method for very, very old browser ordoes not have javascript activated Very customizable and easily expandable, depending on your requirements jQuery and jQuery Validation plugin called from CDNJS to keepnumber of files to maintain at minimal Tools/Credits jQuery jQuery Validation Plugin MailChimp API v3php Wrapper MailChimp API Key and List ID

Below we start, we need to generatepublic API key fromyour MailChimp account and also identifyyour list ID. If you are not sure how to retrievethose, followthese links from MailChimp Knowledge Base to retrieve your account’s API key and find your list ID .

The Form Page (index.php)

jQuery will handle the AJAX using $ . post method and jQuery Validation plugin will validate the field values including email address format.

For this example, I am including first name and last name as part of the form fields. Feel free to make adjustments as required. Althoughkeep in mind after changing the form fields, you may also need to adjust the subscribe . php file too.

<html> <head> <title>MailChimp (API v3) Sign-Up Form</title> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script> <script type="text/javascript"> $(document).ready(function() { // jQuery Validation $("#signup").validate({ // if valid, post data via AJAX submitHandler: function(form) { $.post("subscribe.php", { fname: $("#fname").val(), lname: $("#lname").val(), email: $("#email").val() }, function(data) { $('#response').html(data); }); }, // all fields are required rules: { fname: { required: true }, lname: { required: true }, email: { required: true, email: true } } }); }); </script> </head> <body> <divid="wrapper"> <formid="signup" class="formee" action="subscribe.php" method="post"> <fieldset> <legend>Sign Up</legend> <div> <labelfor="fname">First Name *</label> <inputname="fname" id="fname" type="text"/> </div> <div> <labelfor="lname">Last Name *</label> <inputname="lname" id="lname" type="text"/> </div> <div> <labelfor="email">Email Address *</label> <inputname="email" id="email" type="text"/> </div> <div> <inputclass="right inputnew" type="submit" title="Send" value="Send"/> </div> </fieldset> </form> <divid="response"></div> </div> </body> </html>

Form Processor (subscribe.php)

I try to include as muchcommentas possible to make the code self explanatory.

<?php // Put your MailChimp API and List ID hehe $api_key = 'ENTER_YOUR_API_KEY_HERE'; $list_id = 'ENTER_YOUR_LIST_ID_HERE'; // Let's start by including the MailChimp API wrapper include('./inc/MailChimp.php'); // Then call/use the class use \DrewM\MailChimp\MailChimp; $MailChimp = new MailChimp($api_key); // Submit subscriber data to MailChimp // For parameters doc, refer to: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/ // For wrapper's doc, visit: https://github.com/drewm/mailchimp-api $result = $MailChimp->post("lists/$list_id/members", [ 'email_address' => $_POST["email"], 'merge_fields'=> ['FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"]], 'status'=> 'subscribed', ]); if ($MailChimp->success()) { // Success message echo "<h4>Thank you, you have been added to our mailing list.</h4>"; } else { // Display error echo $MailChimp->getLastError(); // Alternatively you can use a generic error message like: // echo "<h4>Please try again.</h4>"; } ?> Download

Click here to download the basic files to create MailChimp subscribe form on your website. Alternatively, you can also visit the GitHub repository .

Coming Soon Templates

The MailChimp subscribe form exampleonly includes basic HTML page. If you are thinking of using it for your upcoming site, and you have limited resources to create the page design, check out my list of 5 Clean and Free Coming Soon Page Templates and Coming Soon & Under Construction Templates on ThemeForest .

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: jQueryPHPJavaScriptGitHTMLCDNGitHubJava
分页:12
转载请注明
本文标题:MailChimp Subscribe Form with PHP, jQuery and AJAX (API 3.0)
本站链接:http://www.codesec.net/view/522441.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(48)