Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

[Solved] Newsletter subscription error

edited July 2013 in Apps

Pressing "Sign Me Up" this page is called


then I get this error

/cache/newsletter-subscribe.php on line 24: Invalid argument supplied for foreach()

in this file at lines 24,25,26 there is:

    <?php foreach ($data->failed as $data->loop_index => $data->loop_value) { ?>
    $('#<?php echo Template::sanitize ($data->loop_value, 'UTF-8'); ?>-notice').show ();
    <?php } ?>

What goes wrong? Thanks.


  • edited July 2013

    It's trying to read $data->failed, but that variable is (most likely) empty. Without looking at snippets from the handler or view, my best guess would be that it isn't correctly passing the variable from the handler to the view, or it's calling the variable incorrectly.

  • This is the output enabling debug.

    ErrorException: Invalid argument supplied for foreach()
    Debugger::handle_error (2, "Invalid argument supplied for foreach()", "D:\wamp\www\cache\newsletter-subscribe.php", 24, array(7))
    22.         }
    23.     });
    24.     <?php foreach ($data->failed as $data->loop_index => $data->loop_value) { ?>
    25.     $('#<?php echo Template::sanitize ($data->loop_value, 'UTF-8'); ?>-notice').show ();
    26.     <?php } ?>
    require ("D:\wamp\www\cache\newsletter-subscribe.php")
    297.         ob_start ();
    298.         require ($cache);
    299.         return ob_get_clean ();
    300.     }
    Template->render ("newsletter/subscribe", array(2))
    32.         'email' => $_GET['email'],
    33.         'list_id' =>$_GET['list_id']
    34.     ));
    35. }
    require ("D:\wamp\www\apps\newsletter\handlers\subscribe.php")
    442.         // Run the handler and get its output
    443.         ob_start ();
    444.         $res = require ($handler);
    445.         $out = ob_get_clean ();
    446.         if (is_string ($res) && strlen ($res) > 0) {
    Controller->handle ("apps/newsletter/handlers/subscribe.php", false)
    150.     $handler = $controller->route ($_SERVER['REQUEST_URI']);
    151. }
    152. $page->body = $controller->handle ($handler, false);
    154. /**
    Error Context
    $template = "newsletter/subscribe";
    $data = stdClass (
        $email = "mail@domani.tld";
        $list_id = "c2ec48c157";
        $is_being_rendered = true;
    $app = "newsletter";
    $view = "subscribe";
    $file = "apps/newsletter/views/subscribe.html";
    $cache = "cache/newsletter-subscribe.php";
    $out = "<script src="/js/jquery.verify_values.js"></script>
    .notice {
        color: #69c;
        display: none;
    $(function () {
        $('#subscribe-form').verify_values ({
            handler: 'newsletter/subscribe',
            callback: function (failed) {
                // highlight the failed elements
                for (var i = 0; i < failed.length; i++) {
                    $('#' + failed[i] + '-notice').show ();
            reset: function (fields) {
                for (var i = 0; i < fields.length; i++) {
                    $('#' + fields[i] + '-notice').hide ();
        <?php foreach ($data->failed as $data->loop_index => $data->loop_value) { ?>
        $('#<?php echo Template::sanitize ($data->loop_value, 'UTF-8'); ?>-notice').show ();
        <?php } ?>
    <form method="POST" name="subscribe-form" id="subscribe-form">
    <p><?php echo __ ('First Name'); ?>:<br /><input type="text" name="first_name" size="40" value="<?php echo Template::sanitize ($data->first_name, 'UTF-8'); ?>" />
    <span class="notice" id="first_name-notice"><?php echo __ ('You must enter your first name.'); ?></span></p>
    <p><?php echo __ ('Last Name'); ?>:<br /><input type="text" name="last_name" size="40" value="<?php echo Template::sanitize ($data->last_name, 'UTF-8'); ?>" />
    <span class="notice" id="last_name-notice"><?php echo __ ('You must enter your first name.'); ?></span></p>
    <p><?php echo __ ('Email address'); ?>:<br /><input type="text" name="email" size="40" value="<?php echo Template::sanitize ($data->email, 'UTF-8'); ?>" />
    <span class="notice" id="email-notice"><?php echo __ ('You must enter a valid email, or your email may already be registered.'); ?></span></p>
    <input name="list_id" type="hidden" value="<?php echo Template::sanitize ($data->list_id, 'UTF-8'); ?>">
    <input type="submit" value="Subscribe!">

    If someone can help me to debug it with PhpStorm maybe I could provide more info. I tried to debug subscribe.php with argument


    but does not seem to be the correct way because it does not find the class 'Form'. Thanks.

  • hmm... would it be possible to see the handler? I have a theory, but the debug doesn't provide enough info.

  • edited July 2013

    Here it is. Thank you very much.


    $f = new Form ('post', 'newsletter/subscribe');
    $f->verify_csrf = false;
    if ($f->submit ()) {
        //Setup Mailchimp API
        require_once 'apps/newsletter/lib/MCAPI.class.php';
        $settings = parse_ini_file ('conf/newsletter.php');
        $apikey = $settings['mailchimp_api'];
        $api = new MCAPI($apikey);
        //Perform Mailchimp API Call
        $retval = $api->listSubscribe($_POST['list_id'], $_POST['email'], array('FNAME'=>'Jared','LNAME'=>'Short'));
    if ($api->errorCode){
            //If something broke (API)
        } else {
            //Everything worked they should get an email
    } else {
        //Form hasn't been submitted yet.
        $page->title = i18n_get ('Subscribe to the Newsletter');
        echo $tpl->render ('newsletter/subscribe', array(
            'email' => $_GET['email'],
            'list_id' =>$_GET['list_id']
  • edited July 2013

    Well... going through the code on github, the biggest issue I see is that most of the code hasn't really been kept up-to-date and is messy.

    I've played around with it a bit, and that code that's borking it up... seems to be just excess code that shouldn't be there...

    in the views/subscribe.html file remove the following:

    {% foreach failed %}
    $('#{{ loop_value }}-notice').show ();
    {% end %}

    That seemed to solve any issue I had with it. After that, it should work. Just remember that the subscriptions don't show up until the email confirmation has been accepted.

    Also the embeddable object for the newsletter seems a bit buggy, but it could just be with my mock setup. it makes it seem that way...

  • Thank you, solved.

    I also changed line 16 of subscribe.php from

    $retval = $api->listSubscribe($_POST['list_id'], $_POST['email'], array('FNAME'=>'Jared','LNAME'=>'Short'));


    $retval = $api->listSubscribe($_POST['list_id'], $_POST['email'], array('FNAME'=>$_POST['first_name'],'LNAME'=>$_POST['last_name']));

    in order to pass to MailChimp first name and last name inserted by the user.

    Thanks again.

  • Ah yes, forgot about that. The github really should be updated. Anyway, you're welcome.

  • Just submitted a pull request to change that:

  • edited July 2013

    The github is really out of date, and I have little to no time to maintain it. If anyone wants full control, I will gladly hand it over.

    Although, it looks like a rewrite might be more useful.

Sign In or Register to comment.