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

Posted

技术标签:

【中文标题】PHP 注意:未定义索引:将表单提交同步到 mailchimp 列表时【英文标题】:PHP Notice: Undefined index: when syncing form submission to mailchimp list 【发布时间】:2017-07-23 13:58:33 【问题描述】:

我开发了一个 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']);

但是我不确定我应该如何测试/调试这个问题,任何帮助将不胜感激,如果需要,我可以添加更多代码 sn-ps。

【问题讨论】:

尝试检查 isset(),例如(isset($_REQUEST['emailId']) ? $_REQUEST['emailId']: "") 感谢@BrijalSavaliya - 我现在就试试这个 - 但是,从我的第一个 sn-p 代码中,您可以看到这些变量已经设置 - 那么这会有什么不同吗? 所以数据可以通过邮寄方式发送,所以请尝试使用 POST 数据代替 REQUEST 【参考方案1】:

将您所有的 $_REQUEST 更改为 $_POST

$_REQUEST['emailId'] 变为 $_POST['emailId']

【讨论】:

【参考方案2】:

就个人而言,我会从 var_dump($_REQUEST) 或 var_dump($_POST) 开始,以确定这些值是否存在,以及在哪个变量中。

【讨论】:

以上是关于PHP 注意:未定义索引:将表单提交同步到 mailchimp 列表时的主要内容,如果未能解决你的问题,请参考以下文章

PHP 表单抛出错误 - 注意:未定义的索引

Wordpress php未定义索引,表单发布到数据库

PHP会话的未定义索引

表单提交(同步提交)和AJAX提交(异步提交)

未定义的索引:formSignInEmail [关闭]

注意:未定义索引:[重复]中的图像