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."
".
"Phone: ".$phone."
".
"Comment: ".$enquiry."
";
file_put_contents($file,$save, FILE_APPEND);
}
?>
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."
".
"Phone: ".$phone."
".
"Comment: ".$enquiry."
";
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....