未定义的索引:名称

时间:2017-01-14 09:52:13

标签: php html css html5

我在加载表单时不断收到以下消息:


注意:未定义的索引: C:\ xampp \ htdocs \ main.php 中的名称 267


注意:未定义的索引: C:\ xampp \ htdocs \ main.php 中的电子邮件 275


注意 C:\ xampp \ htdocs \ main.php 中的未定义索引:消息 281

我已经查看并按照一些示例来解决此问题但由于某种原因我似乎无法找到解决方案。有什么想法吗?

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $from = 'enquiry'; 
    $to = 'dgdrd@live.com'; 
    $subject = 'Message from Visitor';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">We will get in tocuh soon.</div>';
        } else {
            $result='<div class="alert alert-danger">Error! Try again later.</div>';
        }
    }
}
?>
<form class="form-horizontal" role="form" method="post" action="main.php">
    <div class="form-group">
        <label for="name">name</label>
        <input type="text" id="name" name ="name" class="form-control" placeholder="" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                <?php $errName=''; echo "<p class='text-danger'>$errName</p>";?>

    </div>

    <div class="form-group">
        <label for="email1">email</label>
        <input type="email" id="email" name ="email" class="form-control" aria-describedby="emailHelp" placeholder="" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                <?php $errEmail=''; echo "<p class='text-danger'>$errEmail</p>";?>
    </div>

    <div class="form-group">
        <label for="message">comment</label>
        <textarea class="form-control" rows="6" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                <?php $errMessage=''; echo "<p class='text-danger'>$errMessage</p>";?>
    </div>

    <button type="submit" name="submit" id="submit" class="btn btn-primary btn-wrapper">Send</button>

    <hr>

    <div class="form-group">
        <div class="col-sm-12">
            <?php $result=''; echo $result; ?>  
        </div>
    </div>
</form>

1 个答案:

答案 0 :(得分:1)

它基本上告诉您$_POST不包含任何错误的密钥。您可以在从$_POST

获取价值之前进行一些检查
$name = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name'] : "" ;

它基本上检查$_POST['name']是否为空。有关额外信息,请使用PHPMailer发送邮件,否则您将遇到一些麻烦,例如您的邮件总是发送到垃圾邮件等。