使用 if(isset($_POST['submit'])) 显示消息,但它不起作用



我尝试用PHP编写简单的表单验证。当点击提交按钮,如果名称是空的,那么网站将要求用户填写。但是当我点击提交按钮时,屏幕上什么也没有出现。你能帮我解决这个问题吗?谢谢你!下面是我的代码:

<?php
if (isset($_POST["submit"])){
if(!empty($_POST["Ename"])){
}else{
echo"Please enter your name";
}
}
?>
<html>
<form class="" action="index.php" method="post">
Tutor name: <input type="text" name="Ename" value="">
<input type="button" class="submit" value="Submit your record" name="submit">
</form>
</html>

<input>标签写入<form>标签,设置输入type="submit"

<?php
if (isset($_POST["submit"])) {
if (!empty($_POST["Ename"])) {
} else {
echo "Please enter your name";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form action="" method="POST">
Tutor name: <input type="text" name="Ename" value="">
<input type="submit" class="submit" value="Submit your record" name="submit">
</form>
</body>
</html>

您需要在<form>标记中包装表单输入和提交按钮,如下所示:

<html>
<form method="post">
Tutor name: <input type="text" name="Ename" value="">
<input type="button" class="submit" value="Submit your record" name="submit">
</form>
</html>

最新更新