遇到问题的表单验证,请指出问题所在(PHP验证)
P粉268284930
P粉268284930 2023-09-09 19:34:57
[HTML讨论组]

我想知道在我的代码中表单出了什么问题。

 $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!";
  }
}
?>




    
    
    
    Blank


  


我在本地运行了代码,但是无法得到我想要的结果。

我想要的结果是,如果我没有在“Your Name”字段中输入姓名,那么应该显示结果:

请在登录前先注册

P粉268284930
P粉268284930

全部回复(1)
P粉232793765

如果“Your Name”字段为空,则其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";
                }
            }
        }
    }
}
?>

i used empty() to test if the field empty and i used if-else statements because i want the script to stop if it founds a 'true' condition in your script you can use return; but that will exit the rest of your script

have a nice code :)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号