[英]How to get name to save to mailchimp list with PHP+JS?
I am only getting the email address even though I entered my name on the form. 即使我在表格上输入了我的名字,我也只得到电子邮件地址。 Please forgive me I am not an expert in coding.
请原谅我我不是编码专家。 I only know basic html and I just get some codes I find on the internet.
我只知道基本的html,而我却得到一些我在互联网上找到的代码。
My newsletter form looks like this: 我的新闻稿形式如下:
<form id="subscribe" class="form" action="<?=$_SERVER['PHP_SELF']; ?>" method="post">
<div class="form-group form-inline">
<input size="15" type="text" class="form-control required" id="NewsletterName"
name="NewsletterName" placeholder="Your name" />
<input size="25" type="email" class="form-control required" id="NewsletterEmail"
name="NewsletterEmail" placeholder="your@email.com" />
<input type="submit" class="btn btn-default" value="SUBSCRIBE" />
<span id="response">
<? require_once('assets/mailchimp/inc/store-address.php'); if($_GET['submit']){ echo
storeAddress(); } ?>
</span>
</div>
</form>
my js file looks like this: 我的js文件看起来像这样:
jQuery(document).ready(function() {
jQuery('#subscribe').submit(function() {
// update user interface
jQuery('#response').html('<span class="notice_message">Adding email address...</span>');
var name = jQuery('#NewsletterName').val().split(' ');
var fname = name[0];
var lname = name[1];
if ( fname == '' ) { fname=""; }
if ( lname == '' || lname === undefined) { lname=""; }
// Prepare query string and send AJAX request
jQuery.ajax({
url: 'assets/mailchimp/inc/store-address.php',
data: 'ajax=true&email=' + escape(jQuery('#NewsletterEmail').val()),
success: function(msg) {
if (msg.indexOf("Success") !=-1) {
jQuery('#response').html('<span class="success_message">Success! You are now
subscribed to our newsletter!</span>');
} else {
jQuery('#response').html('<span class="error_message">' + msg + '</span>');
}
}
});
return false;
});
});
and my php file looks like this: 和我的PHP文件看起来像这样:
<?php
function storeAddress(){
require_once('MCAPI.class.php'); // same directory as store-address.php
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('mymailchimpapi');
$merge_vars = Array(
'EMAIL' => $_GET['email'],
'FNAME' => $_GET['fname'],
'LNAME' => $_GET['lname']
);
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "myuniqueid";
if($api->listSubscribe($list_id, $_GET['email'], $merge_vars , $_GET['emailtype']) === true) {
// It worked!
return 'Success! Check your inbox or spam folder for a message containing a
confirmation link.';
}else{
// An error ocurred, return error message
return '<b>Error:</b> ' . $api->errorMessage;
}
}
// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>
I don't know if i should also add 我不知道我是否也应该添加
if($api->listSubscribe($list_id, $_GET['fname'], $merge_vars , $_GET['fname']) === true)
on the php file. 在php文件上。 Does anyone know where the problem is?
有人知道问题出在哪里吗? Or is there something wrong in the JS file?
还是JS文件有问题?
Your ajax query string only includes ajax=true&email=
so $_GET['fname']
will be undefined. 您的ajax查询字符串仅包含
ajax=true&email=
因此$_GET['fname']
是未定义的。 It would help if you did some validation of user input at server for security 如果您对服务器上的用户输入进行了一些验证以确保安全性,这将有所帮助
A simpler way to compile the data from form is to use serialize()
从表单编译数据的一种更简单的方法是使用
serialize()
jQuery('#subscribe').submit(function() {
var formData= $(this).serialize() ;
jQuery.ajax({
url: 'assets/mailchimp/inc/store-address.php',
data: formData,
success: function(....
........................
return false;
});
Reference: serialize() API Docs 参考: serialize()API文档
Fixed!!!! 固定!!!! Thanks Charlietfl for pointing out the problem.
感谢Charlietfl指出问题所在。 I Googled that part and was able to find a solution.
我用Google搜索了那部分,然后找到了解决方案。 I tried adding the serialize() code, but it's giving a page error upon clicking the submit button.
我尝试添加serialize()代码,但单击“提交”按钮后出现页面错误。
I added this on my JS file: 我将此添加到我的JS文件中:
data: 'ajax=true&email=' + escape(jQuery('#NewsletterEmail').val()) + '&fname=' + escape(jQuery('#NewsletterName').val()),
so the whole code is: 所以整个代码是:
jQuery(document).ready(function() {
jQuery('#subscribe').submit(function() {
// update user interface
jQuery('#response').html('<span class="notice_message">Adding email address...</span>');
var name = jQuery('#NewsletterName').val().split(' ');
var fname = name[0];
var lname = name[1];
if ( fname == '' ) { fname=""; }
if ( lname == '' || lname === undefined) { lname=""; }
// Prepare query string and send AJAX request
jQuery.ajax({
url: 'assets/mailchimp/inc/store-address.php',
data: 'ajax=true&email=' + escape(jQuery('#NewsletterEmail').val()) + '&fname=' +
escape(jQuery('#NewsletterName').val()),
success: function(msg) {
if (msg.indexOf("Success") !=-1) {
jQuery('#response').html('<span class="success_message">Success! You are now
subscribed to our newsletter!</span>');
} else {
jQuery('#response').html('<span class="error_message">' + msg + '</span>');
}
}
});
return false;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.