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


undefined index and undefined variable

PHP有问题,我正在用valdiation构建一个表单,但我一直收到这个错误:

指定的值"''u003Cbr/''u003E''n''n''u003Cb''u003ENotice''u003C/b''u003E:未定义的索引:位于''u003Cb ''u003EC:''xamplep''htdocs''navigation.php ''u003C/b ''u003E''u003E''u003Cb''u003E/b''u003Cbr/''u003E''n行的电子邮件"不是有效的电子邮件地址。

更多错误信息:

<form class="form-horizontal" role="form" method="post" action="navigation.tphp">
                                      <div class="form-group">
                                          <label for="name" class="col-sm-2 control-label">Name</label>
                                          <div class="col-sm-10">
                                              <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<br />
<b>Notice</b>:  Undefined index: name in <b>C:'xampp'htdocs'navigation.php</b> on line <b>113</b><br />
">
                                              <br />
<b>Notice</b>:  Undefined variable: errName in <b>C:'xampp'htdocs'navigation.php</b> on line <b>114</b><br />
<p class='text-danger'></p>                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <label for="email" class="col-sm-2 control label">Email</label>
                                          <div class="col-sm-10">
                                              <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<br />
<b>Notice</b>:  Undefined index: email in <b>C:'xampp'htdocs'navigation.php</b> on line <b>120</b><br />
">
                                              <br />
<b>Notice</b>:  Undefined variable: errEmail in <b>C:'xampp'htdocs'navigation.php</b> on line <b>121</b><br />
<p class='text-danger'></p>                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <label for="message" class="col-sm-2 control-label">Message</label>
                                          <div class="col-sm-10">
                                              <textarea class="form-control" rows="4" name="message"><br />
<b>Notice</b>:  Undefined index: message in <b>C:'xampp'htdocs'navigation.php</b> on line <b>127</b><br />
</textarea>
                                              <br />
<b>Notice</b>:  Undefined variable: errMessage in <b>C:'xampp'htdocs'navigation.php</b> on line <b>128</b><br />
<p class='text-danger'></p>                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
                                          <div class="col-sm-10">
                                              <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer" value="<br />
<b>Notice</b>:  Undefined index: human in <b>C:'xampp'htdocs'navigation.php</b> on line <b>134</b><br />
">
                                              <br />
<b>Notice</b>:  Undefined variable: errHuman in <b>C:'xampp'htdocs'navigation.php</b> on line <b>135</b><br />
<p class='text-danger'></p>                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <div class="col-sm-10 col-sm-offset-2">
                                              <!-- Will be used to display an alert to the user -->
                                          <br />
<b>Notice</b>:  Undefined variable: result in <b>C:'xampp'htdocs'navigation.php</b> on line <b>141</b><br />
                                          </div>
                                      </div>
                                 </form>

它所指的代码也是:

<form class="form-horizontal" role="form" method="post" action="navigation.tphp">
                                      <div class="form-group">
                                          <label for="name" class="col-sm-2 control-label">Name</label>
                                          <div class="col-sm-10">
                                              <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                                              <?php echo "<p class='text-danger'>$errName</p>"; ?>
                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <label for="email" class="col-sm-2 control label">Email</label>
                                          <div class="col-sm-10">
                                              <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                                              <?php echo "<p class='text-danger'>$errEmail</p>"; ?>
                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <label for="message" class="col-sm-2 control-label">Message</label>
                                          <div class="col-sm-10">
                                              <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']); ?></textarea>
                                              <?php echo "<p class='text-danger'>$errMessage</p>"; ?>
                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
                                          <div class="col-sm-10">
                                              <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer" value="<?php echo htmlspecialchars($_POST['human']); ?>">
                                              <?php echo "<p class='text-danger'>$errHuman</p>"; ?>
                                          </div>
                                      </div>
                                      <div class="form-group">
                                          <div class="col-sm-10 col-sm-offset-2">
                                              <!-- Will be used to display an alert to the user -->
                                          <?php echo $result; ?>
                                          </div>
                                      </div>
                                 </form> 

验证代码:

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(!$_POST['name']) {
    $errName = 'This Form requires a Name';
}
//Check if email has been entered and is valid
if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $errEmail =   'This Form requires a valid Email';
}

//Check if message has been entered
if(!$_POST['message']) {
    $errMessage = 'This Form requires a Message';
}
//Check if simple anti-spam is incorrect
if($human !== 5) {
    $errHuman = 'Your Answer is in-correct';
}
//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">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>';    
    }
}
    }

此外,如果代码与此有关,请提前感谢!

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

例如。

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

您应该使用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>';
        }
    }
}