我想知道在我的代码表单出错了。
<?php
if(isset($_POST['submit'])) {
$arrayName = array("TeacherA", "TeacherB", "TeacherC" , "TeacherD", "TeacherE");
$minimum = 5;
$maximum = 10;
$name = $_POST['yourName'];
$email = $_POST['yourEmail'];
if(strlen($name) < $minimum) {
echo "Your name should be longer than 5 characters";
}
if(strlen($name) > $maximum) {
echo "Your name should not be longer than 10 characters";
}
if(!in_array($name,$arrayName)){
echo "Please do register with us before you can login";
} else {
echo "Welcome!";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blank</title>
</head>
<body>
<form action="form.php" method="post">
<label>Your Name: </label>
<input type="text" name="yourName"> <br>
<label for="">Your E mail:</label>
<input type="email" name="yourEmail" id=""><br>
<!-- <textarea name="yourMessage" id="" cols="30" rows="10"></textarea><br> -->
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
我运行了localhost,但是,我不能得到我想要的结果。
我想要的结果是如果我没有在"你的名字"中输入名字字段,那么它应该显示结果:
登录前请先注册
如果您的姓名;字段为空,那么它的strlen($name)
应该是0
,并且第一个if语句为真,它将显示Your name should be longer than 5 characters
你可以试试:
<?php
if (isset($_POST['submit'])) {
$arrayName = array("TeacherA", "TeacherB", "TeacherC", "TeacherD", "TeacherE");
$minimum = 5;
$maximum = 10;
$name = $_POST['yourName'];
$email = $_POST['yourEmail'];
if (empty($name)) {
echo "Please do register with us before you can login";
} else {
if (strlen($name) < $minimum) {
echo "Your name should be longer than 5 characters";
} else {
if (strlen($name) > $maximum) {
echo "Your name should be less than 10 characters";
} else {
if (in_array($name, $arrayName)) {
echo "Welcome!";
} else {
echo "Your login is not correct";
}
}
}
}
}
?>
我使用empty()
来测试字段是否为空我使用if-else语句是因为我希望脚本在发现"true"条件时停止在你的脚本中,你可以使用return;
,但这会退出你的脚本的其余部分
有一个漂亮的代码:)
试试这个:
if(!isset($name)){
echo "Please do register with us before you can login";
}
else{
echo "Welcome";
}
也:
<input type="text" name="yourName" required>