-4

好的,我想要实现的是一个非常简单的表单验证,如下所示。

  1. 名称:[必填,最小长度:2,最大长度:255]
  2. 电子邮件:[必填,最小长度:3,最大长度:255,有效的电子邮件格式]
  3. 出生日期:[可选,格式:dd/mm/yyyy]

但是,一旦我单击提交(如果字段为空或已填充),我会在空白页上显示所有回显的错误。

“名称必须至少 2 个字符名称是必需的电子邮件必须至少 3 个字符无效电子邮件电子邮件不能留空”

到目前为止我的代码:

索引.php

<form method="post" action="confirm.php">
Name:<input type="text" name="name" />
email:<input type="text" name="email" />
DOB:<input type="date" name="dob" />
<input type="submit" value="submit" />
</form>

确认.php

<?php

$name = $_POST['$name'];
$email = $_POST['$email'];
$dob = $_POST['$dob'];

$namelen = strlen($email);
$emaillen = strlen($email);
$max = 255;
$minname = 2;
$minemail = 3;

if($namelen<$minname){
    echo"name must be at least 2 characters";
}
elseif($namelen>$max){
    echo"name must be less than 255 characters";
}

if(empty($name)){
    echo"name is required";
}
else{
    continue;
}

if($emaillen<$minemail){
    echo"email must be at least 3 characters";
}
elseif($emaillen>$max){
    echo"email must be less than 255 characters";
}

if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    continue;
}
else{
    echo"invalid email";
}

if(empty($email)){
    echo"email cannot be left empty";
}
else{
    continue;
}

?>

非常感谢您的帮助,谢谢。

4

4 回答 4

1

您的代码中有以下内容:

$name = $_POST['$name'];
$email = $_POST['$email'];
$dob = $_POST['$dob'];

您基本上是在尝试访问未定义的索引。$从键名中删除多余的部分:

$name = $_POST['name'];
$email = $_POST['email'];
$dob = $_POST['dob'];

然后,在下面,你有一些这样的条件:

if(condition == true) {
    continue;
} else {
    // do something
}

实际上没有必要,您可以将其更改为:

if(!condition) {
    // do something
}

此外,最好将错误消息推送到数组 ( $errors) 中,然后循环遍历并显示错误消息。它可能有助于更好地组织您的代码。

修改后的代码如下所示:

if(!empty($_POST)) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $dob = $_POST['dob'];

    $namelen = strlen($name);
    $emaillen = strlen($email);
    $max = 255;
    $minname = 2;
    $minemail = 3;

    if($namelen < $minname){
        $errors[] = "name must be at least 2 characters";
    } elseif($namelen > $max){
        $errors[] = "name must be less than 255 characters";
    }

    if($emaillen < $minemail){
        $errors[] = "email must be at least 3 characters";
    } elseif($emaillen > $max){
        $errors[] = "email must be less than 255 characters";
    }

    if(empty($name)){
        $errors[] = "name is required";
    }

    if(empty($email)){
        $errors[] = "email cannot be left empty";
    }

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $errors[] = "invalid email";
    }

    echo "<ul>";
    foreach ($errors as $error) {
        echo "<li>$error</li>";
    }
    echo "</ul>";

}

它仍然可以改进,但是,这应该让你开始!

于 2013-11-05T14:01:43.613 回答
0

检查 if(!empty($_POST[fieldname])) 然后重定向它,在 javascript 中显示字段为空的警报

于 2013-11-05T15:38:24.547 回答
0

在检查第一个和第二个错误后,您还没有写任何东西让它停止。

此外,在声明continue中没有任何意义(请参阅http://php.net/manual/en/control-structures.continue.php)。if

最后,页面是“空白”的,因为没有 HTML 输出,只有文本。您可能希望将用户重定向回带有错误消息的表单页面。

于 2013-11-05T13:57:03.847 回答
0
$name = $_POST['$name'];
$email = $_POST['$email'];
$dob = $_POST['$dob'];

错了,你必须使用

$name = $_POST['name'];
$email = $_POST['email'];
$dob = $_POST['dob'];

你也可能想换行

$namelen = strlen($email);

$namelen = strlen($name);
于 2013-11-05T13:59:42.693 回答