Mahmoud Al-Nafei
Mahmoud Al-Nafei

Reputation: 231

I have a blank page after submitting 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?

Upvotes: 0

Views: 568

Answers (1)

peixotorms
peixotorms

Reputation: 1283

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/

Upvotes: 1

Related Questions