PHP-在验证失败的字段和错误消息中重新显示具有有效值的表单

纳兹纳克

我创建了一个PHP表单,以接受4个文本字段的名称,电子邮件,用户名和密码,并为这些设置了验证。我的代码当前可以正确验证,并且无论代码是否通过验证,都会显示消息。但是,我希望它在提交时保留正确验证的字段,而那些未通过验证的字段则为空,并显示一条错误消息,详细说明原因。

到目前为止,我有以下代码,主要是form.php:

    <?php
    $self = htmlentities($_SERVER['PHP_SELF']);
    ?>
    <form action="<?php echo $self; ?>" method="post">
        <fieldset>
        <p>You must fill in every field</p>
        <legend>Personal details</legend>
            <?php
            include 'personaldetails.php';
            include 'logindetails.php';
            ?>
            <div>            
                <input type="submit" name="" value="Register" />
            </div>
        </fieldset>
    </form>
    <?php
    $firstname = validate_fname();
    $emailad = validate_email();
    $username = validate_username();
    $pword = validate_pw();
    ?>

我的functions.php代码如下:

<?php
function validate_fname() {
    if (!empty($_POST['fname']))    {
        $form_is_submitted = true;
        $trimmed = trim($_POST['fname']);
        if  (strlen($trimmed)<=150  && preg_match('/\\s/', $trimmed))   {
            $fname = htmlentities($_POST['fname']);
            echo "<p>You entered full name: $fname</p>";
        }   else    {
                echo "<p>Full name must be no more than 150 characters and must contain one space.</p>";
        }   }
        }

function validate_email() {        
    if (!empty($_POST['email']))    {
        $form_is_submitted = true;
        $trimmed = trim($_POST['email']);
        if  (filter_var($trimmed, FILTER_VALIDATE_EMAIL))   {
            $clean['email'] = $_POST['email'];              
            $email = htmlentities($_POST['email']);

            echo "<p>You entered email: $email</p>";
        }   else    {
                echo "<p>Incorrect email entered!</p>";
        }   }   
        }

function validate_username() {
    if (!empty($_POST['uname']))        {
        $form_is_submitted = true;
        $trimmed = trim($_POST['uname']);
        if  (strlen($trimmed)>=5 && strlen($trimmed) <=10)  {
            $uname = htmlentities($_POST['uname']);
            echo "<p>You entered username: $uname</p>";
        }   else    {
                echo "<p>Username must be of length 5-10 characters!</p>";
        }   }   
    }

function validate_pw()  {
    if (!empty($_POST['pw']))   {
        $form_is_submitted = true;
        $trimmed = trim($_POST['pw']);
        if  (strlen($trimmed)>=8 && strlen($trimmed) <=10)  {           
            $pword = htmlentities($_POST['pw']);
            echo "<p>You entered password: $pword</p>";
        }   else    {
                echo "<p>Password must be of length 8-10 characters!</p>";      
        }   }
    }
?>

我如何确保按下提交按钮时,它将保留有效输入,并清空无效的返回错误消息的输入。

最好我还希望初始if(!empty)有一个替代else条件。我最初有这个,但是发现它将以错误信息开始表格。

最后,在通过此表单注册后,如何将有效信息记录到外部文件中以用于检查登录详细信息?

任何帮助是极大的赞赏。

加里

尝试对错误使用单独的变量,而不将错误消息输出到该input字段。

您可以global为此使用变量,但是我不喜欢它们。

login.php

<?php 
$firstname = '';
$password  = '';
$username  = '';
$emailadd  = '';
$response  = '';
include_once('loginprocess.php');
include_once('includes/header.php);
//Header stuff
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8");?>" method="post">
    <fieldset>
        <p>Please enter your username and password</p>
    <legend>Login</legend>
        <div>
        <label for="fullname">Full Name</label>
            <input type="text" name="fname" id="fullname" value="<?php echo $firstname ?>" />
        </div>
        <div>
         <label for="emailad">Email address</label>
         <input type="text" name="email" id="emailad" value="<?php echo $emailadd; ?>"/>
        </div>
        <div>
        <label for="username">Username (between 5-10 characters)</label>
        <input type="text" name="uname" id="username" value='<?php echo $username; ?>' />
        </div>
        <div>            
        <label for="password">Password (between 8-10 characters)</label>
        <input type="text" name="pw" id="password" value="<?php echo $password; ?>" />
        </div>
        <div>            
            <input type="submit" name="" value="Submit" />
        </div>
    </fieldset>
</form>
<?php
//Output the $reponse variable, if your validation functions run, then it
// will contain a string, if not, then it will be empty.
if($response != ''){
    print $response;         
}     
?>
//Footer stuff

loginprocess.php

//No need for header stuff, because it's loaded with login.php
if($_SERVER['REQUEST_METHOD'] == 'POST'){//Will only run if a post request was made.
    //Here we concatenate the return values of your validation functions.
    $response .= validate_fname();
    $response .= validate_email();
    $response .= validate_username();
    $response .= validate_pw();
}    
//...or footer stuff.

functions.php

function validate_fname() {
    //Note the use of global...
    global $firstname;
    if (!empty($_POST['fname']))    {
        $form_is_submitted = true;
        $trimmed = trim($_POST['fname']);
        if(strlen($trimmed)<=150  && preg_match('/\\s/', $trimmed)){
            $fname = htmlentities($_POST['fname']);
            //..and the setting of the global.
            $firstname = $fname;
            //Change all your 'echo' to 'return' in other functions.
            return"<p>You entered full name: $fname</p>";
        } else {
            return "<p>Full name must be no more than 150 characters and must contain one space.</p>";
        }
    }
}

我不建议将include用于诸如表格之类的小东西,我发现它会很快使事情变得一团糟。将所有“显示”代码保存在一个文件中,并且仅在范围发生更改时才将include用于函数(如您所拥有的)和拆分文件。即,您的functions.php文件目前正在处理验证,但是您稍后可能需要创建一个新的include,以处理实际的登录或注册过程。

查看http://www.php.net/manual/en/language.operators.string.php以了解有关串联的信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PHP中的消息队列和工作器系统的有效体系结构?

使用php进行有效的表单处理

仅当继承的类中的实例变量被重新初始化时,具有Singleton Pattern的PHP中的类继承才有效。但为什么?

如何在python和php中使用ZMQ有效地发送单个消息

如何制作有效的PHP / HTML搜索表单?

PHP状态页有效,但Nginx记录错误

在Fluent验证中显示有效值

验证字段后,属性具有最后一个有效值

浮动和具有相同值的字符串,PHP中的错误结果

PHP XML验证在有效XML上失败

PHP中的线程有效吗?

如何使PHP验证表单中的下拉菜单显示“ *必填字段”错误消息?

PHP变量插值,为什么有效

PHP 5.x mail()是否在“答复”字段中验证域的有效性?

具有PHP验证的简单HTML表单

PHP-在php中显示具有特定键和值的关联数组的四项

PHP中的表单验证错误

PHP 表单验证失败

PHP - 有效地显示内联 SVG 图像

PHP/Javascript 验证部分有效

表单操作是另一个页面(php代码)如何验证表单并在视图页面中显示错误消息

我有无法验证的 php 文件以显示正确的错误消息

具有有效和无效类的表单验证

即使表单具有有效值,Angular 4 按钮也被禁用

php - 多个复选框将在数据加载有效值时进行检查

php html 表单有效的电子邮件检查和发布

如何在 PHP 联系表单的每个不同字段中显示错误消息?

类 C# (.NET Core) 中具有“给定值”(有效值列表)的状态字段

可以在 Enum 中添加自定义错误以显示有效值吗?