这种简单的联系表格将无法正常运作

时间:2010-10-01 11:45:39

标签: php forms submit

我创建了一个简单的提交表单,用户输入他们的电子邮件地址并点击提交,他们的电子邮件地址通过电子邮件发送给我,以便在邮件列表中使用。

出于某种原因,它一直给我同样的错误,实际上不会发送电子邮件:

  

很抱歉,但这似乎不是有效的电子邮件地址。

这是我的代码:

<?php define('access', true); ?>
<?php
    // Configuration
    $emailTo = 'myemail@mydomain.com'; // 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'); ?>

3 个答案:

答案 0 :(得分:2)

如果你打开错误报告

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

然后你会得到错误信息:

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

并且那是因为您的表单中没有任何电子邮件。相反,你称之为mailinglist

因此,请将字段名称更改为电子邮件或更改代码以请求mailinglist字段。

答案 1 :(得分:1)

很容易看到。 你的错误在这里:

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

它应该是这样的:

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

答案 2 :(得分:1)

trim($_POST['email'])更改为trim($_POST['mailinglist'])