表单验证工作不正常:名称仍然未填充


Form validation not working properly: name remains unfilled

我是PHP的新手。我有一个非常基本的表格,我试图在上面添加非常基本的验证。我不知道我做错了什么,但一旦我提交了表格,每个字段都填好了,它就不会转到另一页。它将名称显示为未填充的字段。

<?php
error_reporting(0);
if($_POST['submit'])
{
$error=array();
$n=$_POST['name'];
$p=$_POST['password'];
    if($n == '');
    {
        $error[0]= "Please fill a name";
    }
    if($p == '')
    {
        $error[1]= "Password is required";
    }
}
?>
<html>
<form method="post" action="<?php  if($_POST['submit']) { if(count($error)<1) { echo "hello.php"; } else { echo ''; } } ?>">
<div>
<span>Username:</span> <input type="text" name="name" id="user" /> <span style="color:red;"> <?php echo $error[0];?> </span>
</div>
<div>
<span>Password:</span><input type="password" name="password" id="pass" /><span style="color:red;"> <?php echo $error[1];?> </span>
</div>
<div>
<input type="submit" name="submit" value="submit" />
<input type="reset" name="reset" value="reset" />
</div>
</form>
</html>

现在,即使当我提交表格时,每个字段都填写了,我得到的姓名字段也没有填写!这可能是件愚蠢的事,但我想不通。

if($n == '');删除从这条线上看,应该是这个

if($n == '')

这是您在评论中提出的问题//而不是对操作进行编码。在表单中,如果$error消息为空,则应该检查它们。您可以将用户引导到hello.php。谢谢

 <?php
error_reporting(0);
if($_POST['submit'])
{
$error=array();
$n=$_POST['name'];
$p=$_POST['password'];
    if(empty($n))
    {
        $error[0]= "Please fill a name";
    }
    if(empty($p))
    {
        $error[1]= "Password is required";
    }
    if(empty($error))
    {
        header('Location:hello.php');
    }
}
?>
<form method="post" action="">
<div>
<span>Username:</span> <input type="text" name="name" id="user" /> <span style="color:red;"> <?php echo $error[0];?> </span>
</div>
<div>
<span>Password:</span><input type="password" name="password" id="pass" /><span style="color:red;"> <?php echo $error[1];?> </span>
</div>
<div>
<input type="submit" name="submit" value="submit" />
<input type="reset" name="reset" value="reset" />
</div>
</form>
</html>

您可以像一样使用php函数empty()

if(empty($n)){
$error[0]= "Please fill a name";
}

在php中使用以下内容:

<?php
function errors($error) {
    echo '<ul> class="error"';
    foreach($error as $fail) {
        echo '<li>'.$fail.'</li>';
    }
    echo '</ul>';
}
// Sanitize user input
function sanitize($value) {
    // You need to pass your db connection to mysqli functions.
    // You can do this by setting it as a paramater to the function or include a file with the connection in the function (not really recommended)
    return mysqli_real_escape_string($connect, trim(strip_tags($value)));
}
//redirect a user
function to($url) {
   if(headers_sent()) {
       echo '<script>window.location("'.$url.'");</script>';
   } else {
       header('Location: '.$url);
       exit();
   }
}
// The code is only tiggered when the page is posted
if($_POST) {
    $error = array(); // store all errors
    $name = sanitize($_POST['name']);
    $pass = sanitize($_POST['password']);
    if(!empty($name) && !empty($pass)) {
        // continue with more validation
    } else {
        $error[] = 'You didn''t enter a username and or password';
    }
    if(!empty($error)) {
        echo errors($error);
    } else {
        // if there are no errors (forexample log the user in)
        to('hello.php');
    }
}
?>

作为你的html,这就足够了:

<style>
.error ul {
	list-style-type: none;
	margin: 0;
	padding: 0;
}
.error ul li {
	color: red;
}
</style>
<html>
	<form action="" method="post">
		<div>
			<span>Username:</span> <input type="text" name="name" id="user">
		</div>
		<div>
			<span>Password:</span> <input type="password" name="password" id="pass">
		</div>
		<div>
			<input type="submit" value="Submit">
			<input type="reset" value="Reset">
		</div>
	</form>
</html>

一些有用的链接:

  • http://php.net/manual/en/mysqli.real-escape-string.php
  • http://nl1.php.net/manual/en/function.trim.php
  • http://nl1.php.net/manual/en/function.strip-tags.php