-4

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.

user2955356
  • 1
  • 1
  • 1
  • 3

4 Answers4

1

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!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

You have not written anything to make it stop after checking the first and second error.

Also, continue makes no sense in an if statement (see http://php.net/manual/en/control-structures.continue.php).

Lastly, the page is "blank" because there is no HTML output, just the text. You might want to redirect the user back to the form page with the error messages instead.

Community
  • 1
  • 1
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
0
$name = $_POST['$name'];
$email = $_POST['$email'];
$dob = $_POST['$dob'];

That's wrong, you have to use

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

Also you may want to change the line

$namelen = strlen($email);

to

$namelen = strlen($name);
Christine
  • 125
  • 8
0

check if(!empty($_POST[fieldname])) and then redirected it displaying a alert in javascript that the fields are empty

Ribson
  • 15
  • 6