-2
   if($_POST){



$errors = array();


//start validation
if(empty($_POST['email'])){
    $errors['email1'] = "Your Email Cannot be empty"; 
}

if (!filter_var(['email'], FILTER_VALIDATE_EMAIL)){
    $errors['email2'] = "Email not valid!";
}


if(empty($_POST['phone'])){
    $errors['phone1'] = "Your Number Cannot be empty"; 
}
if(strlen($_POST['phone']) < 8){
    $errors['phone2'] = "Your Phone Number must be at least 8 characters long";
}

if(strlen($_POST['phone']) > 8){
    $errors['phone3'] = "Your Phone Number must be not more than 8 characters long";
}

//check errors
if(count($errors) == 0)
{
    //redirect to home page
    header("Location: index.php");
    exit();
}
}

This is my code which needs to be validated. The validation works ok but here is the problem.

<div class="form-group">
            <label for="name">Email</label>
            <input type="email" class ="form-control" name="email" id="email" placeholder="Enter Email">
            <p><?php if(isset($errors['email1'])) echo $errors['email1'];?></p>
            <p><?php if(isset($errors['email2'])) echo $errors['email12'];?></p>
        </div>

        <div class="form-group">
            <label for="phone">Phone Number</label>
            <input type="text" class ="form-control" name="phone" id="phone" placeholder="Enter Phone Number">
            <p><?php if(isset($errors['phone1'])) echo $errors['phone1'];?></p>
            <p><?php if(isset($errors['phone2'])) echo $errors['phone2'];?></p>
            <p><?php if(isset($errors['phone3'])) echo $errors['phone3'];?></p>
        </div>

I have the form set to save to a file. The inputs will be validated and the error messages will be displayed but the form that has wrong inputs will still be saved to a file. Any help would be appreciated thanks.

<?php
    if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry'])){

        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $enquiry = $_POST['enquiry'];

        $file = "form.txt";


        $save = "Email: ".$email."\n".
                "Phone: ".$phone."\n".
                "Comment: ".$enquiry."\n";

        file_put_contents($file,$save, FILE_APPEND);
}
?>
frag
  • 1
  • 2
  • Are you saying the form is saving to the file but you DON'T want it to? Or it's not but you DO want it to? Also I see two PHP files: the first block of code and the third. Which is `index.php`? Are they both in index.php? I don't see a `
    ` element anywhere in your question, so I don't know if the whole thing is `index.php` and submitting to itself or not. Also you have `$errors['email12']` in your HTML section, but now `email12` anywhere else.
    – Bing Jun 04 '17 at 20:56
  • these are 2 php tags in the same contact.php. One is before the form and one is after. I'm trying to validate the code using php. The validation is working ok and I won't be redirected to index.php when the inputs are wrong and I will when there are correct inputs. The problem is that when im entering wrong stuff and pressing submit, the error messages do pop up but the wrong submission will still be put into the text file. Also email2 is in the validation form in the first php tag. – frag Jun 04 '17 at 21:00

1 Answers1

0

Based off your comment, you want ONLY VALID data to go into the file.

But your PHP check is looking to see if ANY values are set, here:

if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry'])) {

If you change that to check to make sure ONLY valid data is present, then you'd be good. Adding a simple && count($errors) == 0 could do it (if I understand the structure of your files).

So just replace that line above with this:

if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry']) && count($errors) == 0) {

So your file would look something like this:

    ....PHP code above here....

    //check errors
    if(count($errors) == 0)
    {
          if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry'])){

                $email = $_POST['email'];
                $phone = $_POST['phone'];
                $enquiry = $_POST['enquiry'];

                $file = "form.txt";

                $save = "Email: ".$email."\n".
                        "Phone: ".$phone."\n".
                        "Comment: ".$enquiry."\n";

                file_put_contents($file,$save, FILE_APPEND);
        }

        //redirect to home page
        header("Location: index.php");
        exit();
    } // end count($errors) == 0 check
} // end if($_POST) check
?>

<div class="form-group">
        <label for="name">Email</label>
        <input type="email" class ="form-control" name="email" id="email" placeholder="Enter Email">
        <p><?php if(isset($errors['email1'])) echo $errors['email1'];?></p>
        <p><?php if(isset($errors['email2'])) echo $errors['email12'];?></p>
    </div>

....HTML code continues here....
Bing
  • 3,071
  • 6
  • 42
  • 81
  • should i remove the if(count($errors) == 0) from the top php tag or no? – frag Jun 04 '17 at 21:06
  • That depends upon what you want it to do. You cannot do a header redirect after printing data, so if you want to redirect to `index.php` after saving the VALID output to the file, I'd actually just move the WHOLE second PHP block just BEFORE the `//redirect to home page` comment line. That way it will still only write to the file and redirect upon success. – Bing Jun 04 '17 at 21:08
  • If you try printing something (to the client, not to a file) via `echo`, `print`, `var_dump`, etc., then you'll run into this problem: https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Bing Jun 04 '17 at 21:10
  • Can I remove the redirect and just have the not saving to file if the data is entered incorrectly? If so where should I put the code exactly. Thanks – frag Jun 04 '17 at 21:14
  • If you don't want the redirect you can just remove that line (and the `exit` after it). I edited the code to give an example of the move/changes. – Bing Jun 04 '17 at 21:16