使用邮件时,仅限一个项目的未定义索引

时间:2013-08-17 02:44:55

标签: php email

我尝试从头开始在PHP中整理联系表单。名称和消息字段都显示正常,它只是发送后未发布的电子邮件地址。

<?php

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

$posted = array(
    'message' => $_POST['message'],
    'name' => $_POST['name'],
    'email_address' => $_POST['email_address']
);

extract($posted);

// to
$to = 'email@example.com'; // need to add ', ' for multiple recipients.

// subject
$subject = 'New message from the PHP sandbox!';

// message
$body = <<<email_body
<html>
    <head>
        <title>New email!</title>
    <head>
    <body>
        <p>
            You have recieved a new email from $name.
        </p>
        <p>
            They said the following:
        </p>
        <p>
            $message
        </p>
    </body>
</html>
email_body;

// headers required for sending html mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// additional headers
$headers .= 'To: My Name <email@example.com>' . "\r\n";
$headers .= 'From: Sandbox <sandbox@localhost>' . "\r\n";

if (mail($to, $subject, $body, $headers)) {
    $status = 'Thanks for your message, we\'ll be in touch shortly!';
}

}

?>

以下是表格:

<!doctype html>
<html lang="en">
<head>
    <title>Contact Form</title>
    <style>
        form ul { margin: 0; padding: 0; }
        form li { list-style: none; margin-bottom: 1em; }
    </style>
</head>
<body>
    <h1>Contact Form</h1>
    <form action="" method="post">
        <ul>
            <li>
                <label for="name">Name</label>
                <input type="text" name="name" id="name">
            </li>
            <li>
                <label for="email_address">Email</label>
                <input type="text" email="email_address" id="email_address">
            </li>
            <li>
                <label for="message">Your Message</label><br>
                <textarea name="message" id="message"></textarea>
            </li>
            <li>
                <input type="submit" value="Go!">
            </li>
        </ul>
    </form>
    <?php if(isset($status)) echo $status; ?>
</body>
</html>

显示的错误是: 注意:未定义的索引:第8行的/Users/x/Sites/learning-php/$_post/index.php中的email_address

任何想法我做错了什么?为什么名称和消息有效而不是电子邮件?

由于

1 个答案:

答案 0 :(得分:1)

这是错误的:

<input type="text" email="email_address" id="email_address">

更改为:

<input type="text" name="email_address" id="email_address">