dongsuishou8039 2016-02-16 21:28
浏览 33
已采纳

提交mailchimp subscribe php后我有一个空白页面

I use bellow html code to submit user email in my mailchimp account list using mailchimp API.

Form Code:

    .....

    <form id="signup-form" action="php/newsletter-subscribe.php" method="post">
        <input type="email" name="email" id="email" placeholder="Email Address" />
        <br>
        <input type="submit" value="Go!" onclick="wating()" />
    </form>

    ......

newsletter-subscribe PHP code:

require_once 'mailchimp/MailChimp.php';
use \DrewM\MailChimp\MailChimp;

// Email address verification
function isEmail($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}

if($_POST) {
    $mailchimp_api_key = 'xxxxxxxxxxxxxxxxxxxxx-xx'; // enter your MailChimp API Key
    $mailchimp_list_id = 'xxxxxxxxxx'; // enter your MailChimp List ID

    $subscriber_email = addslashes( trim( $_POST['email'] ) );
    if( !isEmail($subscriber_email) ) {
        echo '<script type="text/javascript">swal("Error!", "Please try again.", "error")</script>';
    } else {
        $array = array();
        $MailChimp = new MailChimp($mailchimp_api_key);
        $result = $MailChimp->post("lists/$mailchimp_list_id/members", [
            'email_address' => $subscriber_email,
            'status'        => 'subscribed',
        ]);

        if($result == false) {
            $array = '<script type="text/javascript">swal("Error!", "Please try again.", "error")</script>';
        } else {
            $array = '<script type="text/javascript">swal("Great!", "Your email has been subscribed", "success")</script>';
        }
        echo json_encode($array);
    }
}

The problem is after i submit the form i get blank page without any error log and the email added to my mailchimp account without any error.

I try to change the echo javascript in line 22, 35 and 38 with another java script alert like alert("Text Here"); and it's work except i get the same thing blank page

How to solve this problem and echo the javascript alert in the same html form page without redirect to blank page?

  • 写回答

1条回答 默认 最新

  • duangangpin078794 2016-02-16 21:53
    关注

    First you are setting:

    $array = array();
    

    However later, you do:

        if($result == false) {
            $array = '<script type="text/javascript">swal("Error!", "Please try again.", "error")</script>';
        } else {
            $array = '<script type="text/javascript">swal("Great!", "Your email has been subscribed", "success")</script>';
        }
        echo json_encode($array);
    

    In other words, you are changing array to a string. Try to change the code to:

        if($result == false) {
            $array = array('<script type="text/javascript">swal("Error!", "Please try again.", "error")</script>');
        } else {
            $array = array('<script type="text/javascript">swal("Great!", "Your email has been subscribed", "success")</script>');
        }
        echo json_encode($array);
    

    Also, I would avoid using <script type="text/javascript"> there and I would just return a proper json response. Then, you call this file with ajax (jquery), decode the json and show the alert.

    Perhaps something like this will give you an idea: http://labs.jonsuh.com/jquery-ajax-php-json/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀