简单 PHP 登录表单中的未定义索引



我是PHP新手,正在努力开始表单。我收到以下错误 -Notice: Undefined index: email in C:xampphtdocsMattlogin.php on line 7

我需要在代码之前声明空变量吗? 我的代码;

<?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>

$_POST['email'] 变量不存在。您可以使用 isset($_POST['email']( 检查它,然后仅在该值存在时才使用该值。在你的事业中,只需使用 isset 就足够了。

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

最新更新