I have created an enquiry form which allows the user to submit a query and then be emailed a confirmation. That works ok however the last part of the validation does not seem to work so an email is being sent when it shouldn't be.
The data the form is acquiring is the Name, Email and a security question which is very simply to enter a number between 1 & 9. Letters are not accepted as the form has been set to "number" using HTML, however it does accept the number 0 & it also appears to accept any number over the value of 9.
I'm fairly certain my IF statement is incorrect however can't figure out what is wrong. Any help would be greatly appreciated.
The code
<?php
require_once __DIR__ . ('/../config/init.php');
//Checks if data has been posted off .. if so run validation
if($_POST){
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$response = '<h5>Error</h5><p>Please Enter a valid email address</p>';
}
else if (empty($_POST['query'])){
$response = "<h5>Error</h5><p>You have not entered a query. Please go back and try again";
}
else if (empty($_POST['check']) || $_POST['check']<1 && $_POST['check']>=9){
$response = "<h5>Error</h5><p>Number entered not valid. Please go back and try again</p>";
}
else{
$message = 'Thank you for enquiry. We will contact you as soon as possible';
$headers = 'From: webmaster@jamesvause.co.uk' . "
" .
'Reply-To: webmaster@jamesvause.co.uk' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($_POST['email'], 'Your Enquiry', $message, $headers);
$response = '<h3>Thank you for your enquiry</h3>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
include INCLUDES . 'head_tags.php';
?>
</head>
<body>
<div class='container'>
<?php
include INCLUDES . 'header.php';
include INCLUDES . 'nav.php';
?>
<div class='two-thirds column'>
<h2>Contact</h2>
<form action='#' method='post'>
<div>
<?php
if($_POST){
echo $response;
}
?>
</div>
<div>
<label for="email">E-mail:</label>
<input type='email' id="email" name='email' placeholder='Your e-mail address' autocomplete='on' required>
</div>
<div>
<label for="query">Query:</label>
<input type='text' id="query" name='query' placeholder="Please enter your query" required>
</div>
<div>
<label for="check">Enter a number between 1 and 9:</label>
<input type='number' id="check" name='check' maxlength="1" required>
</div>
<input type='submit' value='submit'>
</form>
</div>
</div>
<?php
include INCLUDES . 'footer.php';
?>
</body>
</html>
Many Thanks for your help.