【问题标题】:Notice: Undefined index: variable注意:未定义索引:变量
【发布时间】:2015-08-26 20:10:25
【问题描述】:

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

<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 中的人类 第 25 行

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

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

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

为什么会这样?

【问题讨论】:

  • 因为您从 $_POST 设置为示例名称,并且在检查值为值之前不要使用 isset 函数
  • 这对我有用,但您需要在 $_POST 值上应用 isset()
  • @anantkumarsingh 错了。而且,由于?您需要在您认为无法初始化的每个 var 上使用 isset,例如 php 建议,不一定在 $_POST 上,但仅作为示例在 $name 上(在 OP 情况下)
  • 如果你给定的代码在一个文件中,你肯定会得到这些错误。
  • 我说 $_POST 值,表示他需要检查的每个值。请完整阅读我的评论。

标签: php mysql xampp


【解决方案1】:

代替

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

使用

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

【讨论】:

    【解决方案2】:

    对此,首先你必须使用isset函数进行检查:

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

    像这样,所有变量都一样。

    【讨论】:

      猜你喜欢
      • 2021-03-07
      相关资源
      最近更新 更多