Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php未定义变量注意,为什么我在这里得到它?_Php - Fatal编程技术网

php未定义变量注意,为什么我在这里得到它?

php未定义变量注意,为什么我在这里得到它?,php,Php,我阅读了关于使用三元运算符为未定义变量设置初始默认值的内容。这就是我在脚本顶部试图做的,你会看到的 然而,我仍然在联系人表单的输入值字段中得到未定义的变量错误 有人能解释为什么吗 我的代码: <?php if (isset($_POST["submit"])) { $user_name = isset($_POST["name"]) ? $_POST["name"] : ""; $user_email = isset($_POST["email"]) ? $_PO

我阅读了关于使用三元运算符为未定义变量设置初始默认值的内容。这就是我在脚本顶部试图做的,你会看到的

然而,我仍然在联系人表单的输入值字段中得到未定义的变量错误

有人能解释为什么吗

我的代码:

<?php 

  if (isset($_POST["submit"])) {

    $user_name = isset($_POST["name"]) ? $_POST["name"] : "";
    $user_email = isset($_POST["email"]) ? $_POST["email"] : "";
    $user_age = isset($_POST["age"]) ? $_POST["age"] : "";

    $validation_errors = "";
    $error_counter = null;


    if (empty($_POST["name"])) {
      $validation_errors .= "<li class='validation_errors'>Please input your name.</li>";
      $error_counter++;
    }

    if (empty($user_email)) {
      $validation_errors .= "<li class='validation_errors'>Please input your email.</li>";
      $error_counter++;
    }

    if (empty($user_age)) {
      $validation_errors .= "<li class='validation_errors'>Please input your age.</li>";
      $error_counter++;
    } elseif (intval($user_age) < 18) {
      $validation_errors .= "<li class='validation_errors'>You must be over 18 years old.</li>";
      $error_counter++;
    }


    if ($error_counter != 0) {

      // $validation_errors = "<li class='validation_success'>Message successfully sent!</li>";
      $error_design = "<hr> <p id='error_msg'>Please Fix Below Errors: <p>";

    };

  }

?>


<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8">

  <title>SandBox</title>

  <link rel="stylesheet" type="text/css" href="css/style.css">
  <link rel="stylesheet" href="css/animate.css">

  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/script.js"></script>
</head>
<body>

  <header>

  </header>

  <form action="" method="post">

    <div class="input_group">
      <label for="name">Name</label>
      <input type="text" name="name" value="<?php echo htmlspecialchars($user_name); ?>">
    </div>

    <div class="input_group">
      <label for="email">eMail</label>
      <input type="email" name="email" value="<?php echo htmlspecialchars($user_email); ?>">
    </div>

    <div class="input_group">
      <label for="age">Age</label>
      <input type="text" name="age" value="<?php echo htmlspecialchars($user_age); ?>">
    </div>

    <input type="submit" value="Send" name="submit" id="submit">

    <?php echo $error_design; ?>

    <div>

      <ul>

        <?php echo $validation_errors; ?>

      </ul>

    </div>

  </form>


  <footer></footer>

</body>
</html>

将这些变量放在IF语句之前:

$user_name = isset($_POST["name"]) ? $_POST["name"] : "";
$user_email = isset($_POST["email"]) ? $_POST["email"] : "";
$user_age = isset($_POST["age"]) ? $_POST["age"] : "";
或者直接使用:

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

您是否可以编辑您的问题以包含您收到的错误消息的确切文本,并指出哪些代码行触发了错误?您所需要做的只是在php代码开始时将变量保留为空。在php代码开始时将您的用户名、用户电子邮件和用户年龄设置为空。。。但是也会有一些错误…;)@l0rkaY还有什么错误?只需在开始时将变量设置为空,因为在页面打开时它们没有被设置loads@Gautam你是对的,如果我在检查submit是否被按下之前定义了所有变量,那么它就完全起作用了。所有错误通知现在都已消失。您的答案不正确;这与此无关。再次仔细查看OP的代码,包括他们所写的:“但是我的联系人表单的输入值字段中仍然存在未定义的变量错误。”-解决方案非常简单,但不是您提供的。我在8分钟前编辑了我的答案。。。再次阅读,我已发送解决方案;)“或者只使用://Report all errors,E_NOTICE error_reporting(E_all&~E_NOTICE);”——我相信他们正在使用错误报告,或者他们的系统已经设置为捕获和显示通知。否则,OP就不会发布他们收到未定义的变量通知。不…:D并将这些变量放在IF之前:和。。。。那该怎么修呢?您需要编辑您的答案,以便向OP显示“解决方案”是什么。