注意:未定义的索引:变量

时间:2015-06-11 08:31:51

标签: php mysql xampp

我正在尝试构建以下表单:

<form method="post" action="Index.php">
  <label>Name </label>
  <input name="name" placeholder="Type Here"><br />
  </br>
  <label>Email </label>
  <input name="email" placeholder="Type Here">
  <br /></br>
  <label style="display:block;">I need some information regarding:</label>
  <textarea name="message" placeholder="Type Here"></textarea>
  <br />
  <label>*What is 2+2? (Anti-spam)</label>
  <input name="human" placeholder="Type Here">
  <br />         
  <input id="submit" name="submit" type="submit" value="Submit" style="height:30px;">      
</form>

php:

<?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $from = 'From: TangledDemo'; 
  $to = 'hello@emailaddress.com'; 
  $subject = 'Hello';
  $human = $_POST['human'];

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

  if ($_POST['submit'] && $human == '4') {               
    if (mail ($to, $subject, $body, $from)) { 
      echo '<p>Your message has been sent!</p>';
    } else { 
      echo '<p>Something went wrong, go back and try again!</p>'; 
    } 
  } else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
  }

  if ($_POST['submit']) {
    if ($name != '' && $email != '') {
      if ($human == '4') {               
        if (mail ($to, $subject, $body, $from)) { 
          echo '<p>Your message has been sent!</p>';
        } else { 
          echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
      } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
      }
    } else {
      echo '<p>You need to fill in all required fields!!</p>';
    }
  }
?>

但不断发现这些错误:

  

注意:未定义的索引:C:\ xampp \ htdocs \ bet4info \ Index.php中的名称   第19行

     

注意:未定义的索引:C:\ xampp \ htdocs \ bet4info \ Index.php中的电子邮件   第20行

     

注意:未定义的索引:C:\ xampp \ htdocs \ bet4info \ Index.php中的消息   在第21行

     

注意:未定义的索引:C:\ xampp \ htdocs \ bet4info \ Index.php中的human   第25行

     

注意:未定义的索引:在C:\ xampp \ htdocs \ bet4info \ Index.php中提交   第29行

     

注意:未定义的索引:在C:\ xampp \ htdocs \ bet4info \ Index.php中提交   第35行

     

注意:未定义的索引:在C:\ xampp \ htdocs \ bet4info \ Index.php中提交   第38行

为什么会这样?

2 个答案:

答案 0 :(得分:4)

而不是

$name = $_POST['name'];
if ($_POST['submit'] && $human == '4') {   

使用

$name = isset($_POST['name']) ? $_POST['name'] : '';
if (isset($_POST['submit']) && $human == '4') {   

答案 1 :(得分:0)

首先你需要检查使用isset函数

 `if(isset($_POST['name']) && isset($_POST['email']) &&....) 
  { 
      //if isset than assign data to variable $name = $_POST['name'],...  
  }else{
      //redirect to the error page 
  } `

就像所有变量一样。