开发者

building a MailChimp signup form, get a PHP parse error mystery [closed]

开发者 https://www.devze.com 2023-03-29 18:12 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

I'm working with the code I got from http://apidocs.mailchimp.com/api/downloads/ called "MCAPI Simple Subscribe Example code" -- the one with jQuery.

Locally, where the PHP version is 5.3.5, it works just fine. But when I deploy it to production, where the PHP version is which is 5.2.17, I get this error:

Parse error: syntax error, unexpected T_FUNCTION in /home/myperf7/ public_html/inc/store-address.php on line 1

See it in action at http://myperfectbicycle.com/ -- as you can see, this error is not just aesthetic. It prevents the form from collecting the email address successfully.

Any idea what might be causing that? I can provide further phpinfo() details if needed -- just don't want to spam you all. Here are the contents of store-address.php:

    <?php

    function storeAdd开发者_JAVA技巧ress(){

        // Validation
        if(!$_GET['email']){ return "No email address provided"; } 

        if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
            return "Email address is invalid"; 
        }

        require_once('MCAPI.class.php');
        // grab an API Key from http://admin.mailchimp.com/account/api/
        $api = new MCAPI('XXXXXXX');

        // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
        // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
        $list_id = "XXXXXXX";

        if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
            // It worked!   
            return 'Success! Check your email to confirm sign up.';
        }else{
            // An error ocurred, return error message   
            return 'Error: ' . $api->errorMessage;
        }

    }

    // If being called via ajax, autorun the function
    if(isset($_GET['ajax'])){ echo storeAddress(); }

    ?>

What portion of this code wouldn't work properly in PHP version 5.2? Hmmm...

In the comments somebody asked how the store-address.php file being included; here is it, from my index.php page:

    <form id="signup" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
        <input type="text" value="" name="email" class="email" id="email" placeholder="email address" required="required" />
        <div class="clear">
            <input type="submit" value="Notify me" name="subscribe" class="button"/>
        </div>
        <span id="response"><?php require_once('inc/store-address.php'); if(isset($_GET['submit'])){ echo storeAddress(); } ?></span>
    </form>


Given that the error is on line 1, and looking on your site is says:

Parse error: syntax error, unexpected T_STRING in /home/myperf7/public_html/inc/store-address.php on line 1

I would suggest opening up your store-address.php and looking for any random strings, spaces, newlines, etc. on the first line of the file. Also, it might be a good idea to have the storeAddress() response block on multiple lines.

<span id="response"><?php 
require_once('inc/store-address.php'); 
if(isset($_GET['submit'])){ 
    echo storeAddress(); 
} 

?></span>

The require_once() certainly should be on a separate line.

0

精彩评论

暂无评论...
验证码 换一张
取 消