如何实现嵌入的MailChimp API表单?

时间:2014-05-12 06:00:24

标签: php html css

对于这个或许天真的问题感到抱歉,但我在网页设计方面有绝对的经验,而且我已经连续两天在.html / .css上耕作了,我觉得是时候我只是寻求帮助了。

我正在为会议制作一个简单的目标网页,我一直在使用一些模板。我使用的最新模板是完美的,除了电子邮件注册将直接电子邮件发送到我的帐户,而不是使用mailchimp等服务创建列表 - 非常低效。

我尝试过的前一个模板确实实现了mailchimp,我想知道如何抓住它的代码并将其放入我当前的模板中。

这是来自mailchimp模板的subscribe.php:

<?php
$apiKey = '';
$listId = '';
$double_optin=true;
$send_welcome=true;
$email_type = 'html';
$email = $_POST['email'];
//replace us2 with your actual datacenter
$submit_url = "http://us1.api.mailchimp.com/1.3/?method=listSubscribe";
$data = array(
    'email_address'=>$email,
    'apikey'=>$apiKey,
    'id' => $listId,
    'double_optin' => $double_optin,
    'send_welcome' => $send_welcome,
    'email_type' => $email_type
);
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));
$result = curl_exec($ch);
curl_close ($ch);
$data = json_decode($result);
if ($data->error){
    echo $data->error;
} else {
    echo "Thanks! We'll keep you updated on the conference :)";
}
?>

以下是index.html中的代码片段:

<div class="row">
  <div class="col-md-6 col-sm-12 col-md-offset-3 subscribe">
    <form class="form-horizontal" role="form" action="subscribe.php" id="subscribeForm" method="POST">
      <div class="form-group">
        <div class="col-md-7 col-sm-6 col-sm-offset-1 col-md-offset-0">
          <input class="form-control input-lg" name="email" type="email" id="address" placeholder="Enter your email" data-validate="validate(required, email)" required="required">
        </div>
        <div class="col-md-5 col-sm-4">
          <button type="submit" class="btn btn-success btn-lg">SIGN UP TO BE NOTIFIED</button>
        </div>
      </div>
    </form>
    <span id="result" class="alertMsg"></span> </div>
</div>

供参考,以下是非mailchimp模板中的subscribe.php和index.html代码段:

Subscribe.php

<?php
 if ( isset( $_POST['newsletter_submit'] ) ) {
// Initialize error array
 $newsletter_errors = array();
// Check email input field
 if ( trim( $_POST['newsletter_email'] ) === '' )
 $newsletter_errors[] = 'Email address is required';
 elseif ( !preg_match( "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z] {2,4}$/i", trim( $_POST['newsletter_email'] ) ) )
  $newsletter_errors[] = 'Email address is not valid'; 
  else
  $newsletter_email = trim( $_POST['newsletter_email'] );
// Send email if no input errors
  if ( empty( $newsletter_errors ) ) {
  $email_to = "register@neustep.org"; // Change to your own email address
  $subject = "NeuSTEP Subscription";
  $body = "Subscriber details: " . $newsletter_email . "\r\n";
  mail( $email_to, $subject, $body, $headers );
  echo 'Thank you for subscribing!';
  } else {
  echo 'Please go back and correct the following errors:<br />';
  foreach ( $newsletter_errors as $error ) {
  echo $error . '<br />';
}}}
?>

Index.html代码段

<div class="newsletter">
 <form action="subscribe.php" method="post" id="newsletter-form">
  <p class="form-field">
   <label for="newsletter_email" class="visually-hidden">Your email address</label>
   <i class="icon ion-paper-airplane" aria-hidden="true"></i>
   <input type="text" name="newsletter_email" id="newsletter_email" value="" placeholder="Your email address" />
  </p>
  <p class="form-submit">
   <input type="submit" name="newsletter_submit" id="newsletter_submit" value="Get Notified" />
  </p>
 </form>
</div>

感谢您的帮助!抱歉这篇长篇文章。我不确定stackoverflow上的礼节是什么,所以如果我问一个蹩脚的问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

您的表单没问题,您需要实现订阅处理程序,例如;

<强> subscribe.php

<?php
if (empty($_POST["newsletter_email"]) {
    die("You should provide email!");
} else if (!preg_match( "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z] {2,4}$/i", trim( $_POST['newsletter_email'] ) )) {
    die("Email address is not valid!");
} else {
    $newsletter_email = trim( $_POST['newsletter_email'] );
}

$apiKey = '';
$listId = '';
$double_optin=true;
$send_welcome=true;
$email_type = 'html';
$email = $newsletter_email;
//replace us2 with your actual datacenter
$submit_url = "http://us1.api.mailchimp.com/1.3/?method=listSubscribe";
$data = array(
    'email_address'=>$email,
    'apikey'=>$apiKey,
    'id' => $listId,
    'double_optin' => $double_optin,
    'send_welcome' => $send_welcome,
    'email_type' => $email_type
);
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));
$result = curl_exec($ch);
curl_close ($ch);
$data = json_decode($result);
if ($data->error){
    echo $data->error;
} else {
    echo "Thanks! We'll keep you updated on the conference :)";
}
?>