Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

PHP Simple Form Validation

Ok, what I'm trying to achieve is a very simple form validation like the following.

  1. Name: [required, min length: 2, max length: 255]
  2. Email: [required, min length: 3, max length: 255, valid email format]
  3. Date of Birth: [optional, format: dd/mm/yyyy]

However, once i click submit (either if the fields are empty or filled) I get all of my echoed errors displayed on a blank page.

"name must be at least 2 charactersname is requiredemail must be at least 3 charactersinvalid emailemail cannot be left empty"

My code so far:

index.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>

and

confirm.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;
}

?>

Help would be greatly appreciated, thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have the following in your code:

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

You're basically trying to access undefined indexes. Remove the extra $ from the key names:

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

Then, further below, you have some conditions like this:

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

It's actually not necessary, and you could change it to:

if(!condition) {
    // do something
}

Also, it's better to push the error messages into an array ($errors) and then loop through it and display the error messages. It might help organize your code a bit better.

Here's how the modified code looks like:

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>";

}

It could still be improved, but however, this should get you started!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...