即使我在表单上输入了我的姓名,我也只收到了电子邮件地址。请原谅我,我不是编码专家。我只知道基本的HTML,我只是得到一些我在互联网上找到的代码。
我的简报形式如下:
<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>
我的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;
});
});
我的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(); }
?>
我不知道我是否也应该添加
if($api->listSubscribe($list_id, $_GET['fname'], $merge_vars , $_GET['fname']) === true)
在php文件上。有谁知道问题在哪里?或者JS文件中有什么问题吗?
答案 0 :(得分:2)
您的ajax查询字符串仅包含ajax=true&email=
,因此$_GET['fname']
将是未定义的。如果您在服务器上对安全性进行了一些用户输入验证,那将会有所帮助
从表单编译数据的一种更简单的方法是使用serialize()
jQuery('#subscribe').submit(function() {
var formData= $(this).serialize() ;
jQuery.ajax({
url: 'assets/mailchimp/inc/store-address.php',
data: formData,
success: function(....
........................
return false;
});
答案 1 :(得分:0)
固定!!!!感谢Charlietfl指出了问题所在。我用Google搜索了那部分,并找到了解决方案。我尝试添加serialize()代码,但是在单击提交按钮时出现页面错误。
我在我的JS文件中添加了这个:
data: 'ajax=true&email=' + escape(jQuery('#NewsletterEmail').val()) + '&fname=' + escape(jQuery('#NewsletterName').val()),
所以整个代码是:
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;
});
});