简单的PHP登录格式的未定义索引

时间:2018-09-07 23:14:58

标签: php html

我是PHP的新手,正在努力启动表单。我收到以下错误- Notice: Undefined index: email in C:\xampp\htdocs\Matt\login.php on line 7

我是否需要在代码之前声明空的var。 我的代码;

<?php
  if (array_key_exists("submit", $_POST)) {
    print_r($_POST);
  }

  if (!$_POST['email']) {
      $error .= "An Email is required<br>";
  }
  if ($error != "") {
    $error = "<p>There were error(s) in your form:</p>".$error;
  }
?>

<div class="error"><?php echo $error; ?></div>
<form method="post" id="logInForm">
  <input type="email" class="form-control" name="email" id="email" placeholder="email">
  <input type="password" class="form-control" name="password" id="password" placeholder="password">
  <div class="checkbox pt-2">
    <label>
      <input type="checkbox" name="stayLoggedIn" value="1"> Stay Logged In
    </label>
  </div>
  <input type="submit" name="submit" class="btn btn-primary btn-block mb-4" value="Log in!">
</form>

1 个答案:

答案 0 :(得分:2)

$ _ POST ['email']变量不存在。您可以使用isset($ _ POST ['email'])检查它,然后仅在该值存在时使用它。在您的原因中,仅使用isset就足够了。

if(!isset($_POST['email'])) {
   $error .= "A password is required<br>";
}