2016-05-13 53 views
0

一直與PHP的問題,我在建設有valdiation形式,但我不斷收到此錯誤:未定義指數和未定義的變量

The specified value "\u003Cbr /\u003E\n\u003Cb\u003ENotice\u003C/b\u003E: Undefined index: email in \u003Cb\u003EC:\xampp\htdocs\navigation.php\u003C/b\u003E on line \u003Cb\u003E120\u003C/b\u003E\u003Cbr /\u003E\n" is not a valid email address.

更多的錯誤信息:

<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="[email protected]" 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="[email protected]" 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 = '[email protected]'; 
$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>';  
    } 
} 
    } 

此外,代碼正在包括,如果這與它有關,它提前謝謝你!

回答

0

的問題是,您試圖訪問不存在POST超級全局的值。要解決此問題,您需要運行isset()檢查以查看值是否存在。

例如。

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

您應該使用isset()檢查,如果存在變量:

<?php 

if (!empty($_POST["submit"])) { 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 
    $human = intval($_POST['human']); 
    $from = 'Contact Form'; 
    $to = '[email protected]'; 
    $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>'; 
     } 
    } 
}