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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题