tags:

views:

39

answers:

3

I've created a simple submit form, where the user inputs their e-mail address and clicks submit and their e-mail address is e-mailed to me for use in a mailing list.

For some reason it keeps giving me the same error and won't actually send the e-mail:

Sorry but that doesn't seem to be a valid e-mail address.

Here's my code:

<?php define('access', true); ?>
<?php
    // Configuration
    $emailTo = '[email protected]'; // The e-mail address
    $mySite = 'test Mailing List'; // The name of the website
    $subject = 'test | New Mailing Lister!';
    $msg = '';

    // If the form is submitted
    if(isset($_POST['submit'])) {
        // Check that the email entered is valid and not empty
        if(trim($_POST['email']) == '') {
            $hasError = true;
            $msg = '<p class="error">Sorry but that doesn\'t seem to be a valid e-mail address.</p>';
        } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
            $hasError = true;
            $msg = '<p class="error">Sorry but that doesn\'t seem to be a valid e-mail address.</p>';
        } else {
            $email = trim($_POST['email']);
        }

        // Are we error free? If so send away
        if(!isset($hasError)) {
            $body = "Someone has signed up for the test mailing list, the e-mail address they used is:\n\n<strong>$email</strong>";
            $headers = 'From: ' . $mySite . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
            $msg = '<p class="thanks">Thanks for subscribing to test\'s services mailing list. We\'ll keep you posted on service updates!</p>';

            mail($emailTo, $subject, $body, $headers);
            $emailSent = true;
        }
    }
?>
<?php include('includes/html_header.php'); ?>
    <?php include('includes/header.php'); ?>

    <div id="container">
        <?php if(!isset($_GET['id'])) { ?>
        <div id="container_column-1">
            <h1>test</h1>
            <p>Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot Europa usa li sam vocabular. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilite de un nov lingua franca: On refusa continuar payar custosi traductores. At solmen va esser necessi far uniform grammatica, pronunciation e plu sommun paroles. Ma quande lingues coalesce, li grammatica del resultant lingue es plu simplic e regulari quam ti del coalescent lingues. Li nov lingua franca va esser plu simplic e regulari quam li existent Europan lingues. It va esser tam simplic quam Occidental in fact, it va esser Occidental. A un Angleso it va semblar un simplificat Angles, quam un skeptic Cambridge amico dit me que Occidental es. Li Europan lingues es membres del sam familie.</p>
        </div>

        <div id="container_column-2">
            <h1>Coming <span>soon</span>!</h1>
            <p>We have an exciting new service to offer to our customers. We're not able to announce anything right now, but watch this space for updates in the very near future!</p>
            <p>Alternatively, you can enter your e-mail address below to be informed of any new services and offers we have. Don't worry, we won't sell on your e-mail address and you won't receive any junk mail!</p>

            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <label>Be the first to hear about new services!</label>
                <input type="text" size="35" name="mailinglist" id="mailinglist" title="Enter your e-mail address" />
                <button type="submit" name="submit">&raquo;</button>
            </form>
            <?php if(isset($_POST['submit'])) { echo $msg; } ?>
        </div>
        <?php
            } else {
                include($_GET['id'] . ".php");
            }
        ?>
    </div>

    <?php include('includes/footer.php'); ?>
<?php include('includes/html_footer.php'); ?>
+2  A: 

if you would turn on error repoting

<?php
error_reporting(E_ALL);
ini_set('display_errors','on');

then you would end up with the error message:

Notice: Undefined index: email in %script% on line %line%

and thats because you don't have any email filed in your form. instead you called it mailinglist

so change the field name to email or change your code to ask for a mailinglist field.

maggie
+1  A: 

It's pretty easy to see. Your error is here:

<input type="text" size="35" name="mailinglist" id="mailinglist" title="Enter your e-mail address" />

it should be this:

<input type="text" size="35" name="email" id="mailinglist" title="Enter your e-mail address" />
ITroubs
Damn, why did I overlook that. I must of tried everything else but that! Thanks!
Chrish
i had some similar problems constructing my forms and always having the forms populated with the input from my db and from the post inputs but after a while you will become used to it and maybe you even will decide to use codeigniter which has some functionality specially to create forms
ITroubs
+1  A: 

change trim($_POST['email']) to trim($_POST['mailinglist']).

Yogesh