PHP注意:未定义的索引:将表单提交同步到mailchimp列表时

时间:2017-03-03 12:43:05

标签: php jquery ajax wordpress mailchimp-api-v3.0

我开发了一个wordpress插件,它是一个表单生成器。该插件允许用户输入列表ID,API ID,名字,姓氏和电子邮件地址变量,以便在提交表单时将表单提交同步到选定的mailchimp列表。

我有这个工作,但最近注意到它不再向mailchimp发送信息。

检查我的调试文件,我收到以下错误:

[03-Mar-2017 11:53:47 UTC] PHP Notice:  Undefined index: emailId in .../plugins/layers-form-builder/includes/class-send-form.php on line 37
[03-Mar-2017 11:53:47 UTC] PHP Notice:  Undefined index: emailSubject in .../plugins/layers-form-builder/includes/class-send-form.php on line 38
[03-Mar-2017 11:53:47 UTC] PHP Notice:  Undefined index: mcListID in .../plugins/layers-form-builder/includes/class-send-form.php on line 41
[03-Mar-2017 11:53:47 UTC] PHP Notice:  Undefined index: mcAPIkey in .../plugins/layers-form-builder/includes/class-send-form.php on line 42
[03-Mar-2017 11:53:47 UTC] PHP Notice:  Undefined index: formData in .../plugins/layers-form-builder/includes/class-send-form.php on line 54

这是在提交表单时触发的脚本,它通过ajax处理数据:

<script>
jQuery(document).ready(function($) {
    $('#layers-widget-form_builder-17 .builderForm').each(function() {
        $(this).on("submit", function(event) {
            event.preventDefault();
            var formData = $(this).serialize();
            var emailId ='sam.skirrow@gmail.com';
            var emailSubject ='New website form submission';
            var mcListID ='2d2fbcd34d';
            var mcAPIkey ='7f306818ee8b588162a7be1ac72d213b-us10';
                                var mcEmail = $(this).find('input[name="email"]').val(); 
                                                var mcLname = $(this).find('input[name="last_name"]').val();
                                                var mcFname = $(this).find('input[name="first_name"]').val(); 
                            var mcForm = $(this);

            $('#layers-widget-form_builder-17 .builderForm').addClass('loading');
            $.ajax({
                    type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
                    url         : 'http://thedistractionsband.co.uk/wp-content/plugins/layers-form-builder/includes/class-send-form.php', // the url where we want to POST
                    data        : {'formData' :formData,'emailId':emailId, 'emailSubject':emailSubject, 'mcListID':mcListID, 'mcAPIkey':mcAPIkey, 'mcEmail':mcEmail, 'mcFname':mcFname,  'mcLname':mcLname}, // our data object
                    dataType    : 'json', // what type of data do we expect back from the server
                    encode      : true
                })
                .done(function(data) {
                    // log data to the console so we can see
                    console.log(data); 
                    //setTimeout(function(){$('#overlay').hide();}, 4000);
                    //$('#overlay').remove();
                    $('.successMsg').html('<i class="fa fa-check"></i> Your message has been sent!');
                    $('.form-builder-submit').hide();
                    setTimeout(function(){
                        $('.successMsg').hide();
                        $('.builderForm').removeClass('loading');
                        $('.form-builder-submit').show();}, 3000);
                    // Reset form after submission
                    $(".builderForm")[0].reset();
                })
                .fail(function (jqXHR, textStatus) {
                    console.log(textStatus);
                    $('.successMsg').html('<i class="fa fa-cross"></i> Oops, there seems to have been an error when submitting the form');
                    setTimeout(function(){$('.successMsg').hide();}, 3000);
                });

            event.preventDefault();




        });
    });

});

</script>

以下是class-send-form.php(包含错误的文件)

的内容
<?php
/* Layers Form Builder Plugin
 * Script to send custom form 
 */
require_once('../../../../wp-load.php');

// function to send data to mailchimp
function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME' => '','LNAME' => '') ){
$data = array(
    'apikey'        => $api_key,
    'email_address' => $email,
    'status'        => $status,
    'merge_fields'  => $merge_fields
);
$mch_api = curl_init(); // initialize cURL connection

curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
curl_setopt($mch_api, CURLOPT_POST, true);
curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

    $result = curl_exec($mch_api);
    return $result;
}

function getIfSet(&$value, $default = '')
{
    return isset($value) ? $value : $default;
}

//$layers_email_address = Layers_Form_Builder_Widget::send_to_email(); 
$layers_email_address = $_REQUEST['emailId'];
$layers_email_subject = $_REQUEST['emailSubject'];

// mailchimp vars
$mc_list_id = $_REQUEST['mcListID'];
$mc_api_key = $_REQUEST['mcAPIkey'];
$mc_email = getIfSet($_REQUEST['mcEmail']); 
$mc_fname = getIfSet($_REQUEST['mcFname']); 
$mc_lname = getIfSet($_REQUEST['mcLname']); 

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;

} else {
parse_str($_REQUEST['formData'], $formFields);
$html='<html><body>';
foreach ($formFields as $key => $value) {
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}
$html .='</body></html>';
$to = $layers_email_address;
$subject = $layers_email_subject;
$txt = $html;
$headers = "From: <".$to.">". "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."\r\n";
wp_mail($to,$subject,$txt,$headers);

//mailchimp test
$email = $mc_email;
$status = 'subscribed'; // "subscribed" or "unsubscribed" or "cleaned" or "pending"
$list_id = $mc_list_id; // where to get it read above
$api_key = $mc_api_key; // where to get it read above
$merge_fields = array('FNAME' => $mc_fname,'LNAME' => $mc_lname);

rudr_mailchimp_subscriber_status($email, $status, $list_id, $api_key, $merge_fields );    

// if there are no errors process our form, then return a message

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);

我的问题似乎存在于以下几个方面:

$layers_email_address = $_REQUEST['emailId'];
$layers_email_subject = $_REQUEST['emailSubject'];

// mailchimp vars
$mc_list_id = $_REQUEST['mcListID'];
$mc_api_key = $_REQUEST['mcAPIkey'];
$mc_email = getIfSet($_REQUEST['mcEmail']); 
$mc_fname = getIfSet($_REQUEST['mcFname']); 
$mc_lname = getIfSet($_REQUEST['mcLname']);

但是我不确定如何测试/调试此问题,我们非常感谢任何帮助,如果需要,我可以添加更多代码段。

2 个答案:

答案 0 :(得分:0)

将您的所有string更改为$_REQUEST&#39>

$_POST变为$_REQUEST['emailId']

答案 1 :(得分:0)

就个人而言,我开始使用var_dump($ _ REQUEST)或var_dump($ _ POST)来确定这些值是否存在,以及在哪个变量中存在。