我是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>
现在即使当我提交表格时,每个字段都填满了我得到名字字段没有填写! 这可能是一些愚蠢的事情,但我无法弄明白。
答案 0 :(得分:0)
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>
答案 1 :(得分:0)
在你的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>
一些有用的链接:
答案 2 :(得分:0)
您可以使用php函数empty(),如
if(empty($n)){
$error[0]= "Please fill a name";
}