未定义的索引和未定义的变量

时间:2016-05-13 11:35:51

标签: php

遇到PHP问题,我正在构建一个带有valdiation的表单,但我不断收到此错误:

  

指定的值" \ u003Cbr / \ u003E \ n \ u003Cb \ u003ENotice \ u003C / b \ u003E:未定义的索引:电子邮件在\ u003Cb \ u003EC:\ xampp \ htdocs \ navigation.php \ u003C / b \ u003E在线\ u003Cb \ u003E120 \ u003C / b \ u003E \ u003Cbr / \ u003E \ n"不是有效的电子邮件地址。

更多错误信息:

int get(); // Function

它所指的代码:

int get; // Variable

验证码:

int get(0); // int constructor takes one argument 

此代码也包括在内,如果与此有关,请提前感谢您!

2 个答案:

答案 0 :(得分:0)

您应该使用isset()检查变量是否存在:

<?php

if (!empty($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $from = 'Contact Form';
    $to = 'schlitzws@gmail.com';
    $subject = 'New Contact Message';

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


//Check if name has been entered
    if (!isset($_POST['name'])) {
        $errName = 'This Form requires a Name';
    }

//Check if email has been entered and is valid
    if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'This Form requires a valid Email';
    }


//Check if message has been entered
    if (!isset($_POST['message'])) {
        $errMessage = 'This Form requires a Message';
    }
//Check if simple anti-spam is incorrect
    if (isset($human) && $human !== 5) {
        $errHuman = 'Your Answer is in-correct';
    }

//If there are no errors, send the email
    if (!isset($errName) && !isset($errEmail) && !isset($errMessage) && !isset($errHuman)) {
        if (mail($to, $subject, $body, $from)) {
            $result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
}

答案 1 :(得分:0)

问题是您正在尝试访问POST超全局中不存在的值。要解决此问题,您需要运行isset()检查以确定该值是否存在。

例如

if (isset($_POST["answer"])) { echo $_POST["answer"] }