0

我试着从头开始在 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

任何想法我做错了什么?为什么名称和消息有效,但电子邮件无效?

谢谢

4

1 回答 1

1

这是错误的:

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

改成这样:

<input type="text" name="email_address" id="email_address">
于 2013-08-17T02:49:26.373 回答