0
<?php
//Form Validation
if(isset($_POST['register']))
{
        //Must be at least 4-15 characters and contain letters and numbers
        if(!preg_match('/^[a-zA-Z0-9]{4,15}$/', $_POST['username']))
        {
            $error[]='The username does not match the requirements';        
        }
        //Password validation: must contain at least 1 letter and number. Allows characters !@#$% and be 6-15 characters
        if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,15}$/', $_POST['password1']))
        {
            $error[]='The password does not match the requirements';
        }
        //Email validation
        if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
        {
            $error[]='Invalid E-mail';
        }       
        //Output error in array as each line
        foreach ($error as $output)
        {
            echo "$output <br>";
        }
}
if ((empty($errors)) && !isset($_POST['register']))
{
?>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
    <input type="text" placeholder="Username" maxlength="15" name="username" value="<?php echo $_POST['username']; ?>" /><br>
    <input type="password" maxlength="15" name="password1" /><br>
    <input type="password" maxlength="15" name="password2" /><br>
    <input type="text" placeholder="your@email.com" maxlength="25"  name="email" value="<?php echo $_POST['email']; ?>"/><br>
    <input type="text" maxlength="20" name="county" /><br>
    <input type="submit" value="Register" name="register"/>
</form>
<?php
}
?>

大家好,

我已经到了没有显示错误的地步,但我可能没有清楚地解释自己。在不再显示错误消息之后,我希望表单不再出现,然后我可以写下“成功”之类的内容。但是我似乎无法做到这一点。

4

2 回答 2

2
<?php

$sent = false;

if ( isset($_POST['register']) ) 
    {
        $error = array();

        //Must be at least 4-15 characters and contain letters and numbers
        if(!preg_match('/^[a-zA-Z0-9]{4,15}$/', $_POST['username']))
        {
            $error[]='The username does not match the requirements';        
        }
        //Password validation: must contain at least 1 letter and number. Allows characters !@#$% and be 6-15 characters
        if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,15}$/', $_POST['password1']))
        {
            $error[]='The password does not match the requirements';
        }
        //Email validation
        if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
        {
            $error[]='Invalid E-mail';
        }       

        if ( count($error) > 0) 
        {
            foreach ($error as $output) {
               echo "{$output} <br>";
            }
        } else {
            $sent = true;
        }
    }//end isset register

?>

<?php if ($sent==false) { ?>

    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
        <input type="text" placeholder="Username" maxlength="15" name="username" value="<?php if (isset($_POST['username'])) {echo $_POST['username'];} ?>" /><br>
        <input type="password" maxlength="15" name="password1" /><br>
        <input type="password" maxlength="15" name="password2" /><br>
        <input type="text" placeholder="your@email.com" maxlength="25"  name="email" value="<?php if (isset($_POST['email'])) {echo $_POST['email'];} ?>"/><br>
        <input type="text" maxlength="20" name="county" /><br>
        <input type="submit" value="Register" name="register"/>
    </form>

<?php } else { echo "Success!"; } ?>
于 2013-09-16T22:21:06.847 回答
0

我相信您正在尝试创建一个表单,您可以在其中接受用户的输入并显示错误,直到所有错误都得到修复。您的想法很好,但是您的代码中存在一些逻辑错误。

以下是它的工作原理:

  • 先显示表格
  • 接受用户输入
  • 检查电子邮件和密码
  • 如果有任何错误,请显示它们

这些是我在您的代码中所做的更改:

  • 删除了if(empty($errors)). 这是不必要的
  • 更改if(!isset($_POST['register']))if(isset($_POST['register']))-- 如果表单未提交,我们希望显示该表单。
  • 在字段内添加isset检查<input以确保仅在定义变量时才显示它们
  • 删除了最后一个代码块,}因为它不再需要

尝试这个:

<?php

if(isset($_POST['register']))
{
    $error = array(); //initializing an empty array

    //Must be at least 4-15 characters and contain letters and numbers
    if(!preg_match('/^[a-zA-Z0-9]{4,15}$/', $_POST['username']))
    {
        $error[]='The username does not match the requirements';        
    }
    //Password validation: must contain at least 1 letter and number. Allows characters !@#$% and be 6-15 characters
    if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,15}$/', $_POST['password1']))
    {
        $error[]='The password does not match the requirements';
    }
    //Email validation
    if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
    {
        $error[]='Invalid E-mail';
    }       
    //Output error in array as each line
    foreach ($error as $output)
    {
        if(isset($output)) {
            echo "$output <br>";
        }
    }

}
    if(!isset($error)) {
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
    <input type="text" placeholder="Username" maxlength="15" name="username" 
    value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>" /><br>
    <input type="password" maxlength="15" name="password1" /><br>
    <input type="password" maxlength="15" name="password2" /><br>
    <input type="text" placeholder="your@email.com" maxlength="25"  name="email" 
    value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/><br>
    <input type="text" maxlength="20" name="county" /><br>
    <input type="submit" value="Register" name="register"/>
</form>

<?php

} 

else {
    echo "Successful";
}

?>
于 2013-09-16T22:16:44.287 回答