Jump to content

Archived

This topic is now archived and is closed to further replies.

tgavin

Can't find echo error

Recommended Posts

I have a textarea that allows for multiple email address insertion, each address on its own line. There are two checks; address validation and duplicate checks. When each check is run on its own the info echoed is correct.
However, when both checks are triggered the 'invalid format (bad)' check echos what appears to be blank spaces left over from email addresses that passed and were inserted.

I've tried to unset(), among other things but can't figure out where the "problem" is happening. Again, when triggered by itself it echos fine.[code]<?php
if(isset($_POST['addMultiple_x'])) {

    $newemails = trim($_POST['emails']);
    
    if(!$newemails) {
        // textarea was left blank
        alert('\nIt looks like you were attempting to enter multiple test email addresses, although the text field looks empty.\n\nPlease double-check that at least one email address is entered and try again.','subs_add.php');
        exit;
    }

    $emails = preg_split('/[\r\n]+/', $newemails);

    foreach($emails as $email) {

        // check for duplicate email addresses
        if($_POST['check_dups'] == 1) {
            email_check($email);
            if($email_check > 0) {
                // email address already exists!
                $dup_error = 1;
                $dup_email[] = $email;
                unset($email);
            }
        }

        // check for bad email formatting / syntax.
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $bad_error = 1;
            $bad_email[] = $email;
            unset($email);
        } else {
            // email passed inspection, insert it
            // basic insert queries here
        }
    }

    // success/failure messages
    function dup_error($dup_email) {
        echo "<span class=\"header\" style=\"float:center\">Duplicates Found</span><br /><strong>The following email address(es) are already in the list and were not inserted</strong><br /><br />";
        foreach($dup_email as $value) {
            echo $value.'<br />';
        }
    }
    
    function bad_error($bad_email) {
        echo "<span class=\"header\" style=\"float:center\">Invalid Format</span><br /><strong>The following email address(es) is/are invalid and were not inserted</strong><br /><br />";
        foreach($bad_email as $value) {
            echo $value.'<br />';
        }
    }
    if($dup_error && $bad_error) {
        dup_error($dup_email);
        echo '<br /><hr><br />';
        bad_error($bad_email);
        exit;
    }
    
    // there were duplicate addresses
        elseif($dup_error) {
        dup_error($dup_email);
        exit;
    }
    
    // there were some bad emails
    elseif($bad_error) {
        bad_error($bad_email);
    exit;
    
    } else {
    // all emails passed verification
    header("Location: success.php?msg=addMultiple&precount=$precount");
    exit;
    }
}
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.