I'm trying to validate several form items using PHP, including a full name, a phone number, an email address, a checkbox and a radio button set. I've started with email (using FILTER_VALIDATE_EMAIL) and phone (using a regular expression), but have come across several errors that I can't figure out. Below is the code for the PHP for both validation functions and the corresponding HTML:
PHP:
<?php
$phone_validate = "";
$email_validate = "";
$phone_expr = '^\(?([0-9]{3})\)?[-]?([0-9]{3})[-]?([0-9]{4})$';
function validatePhone ($input){
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (preg_match($phone_expr, $input) == 1){
return "This phone is valid.";
}
else {
return "This phone is not valid.";}
}
}
function validateEmail ($input){
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ((!filter_var($input, FILTER_VALIDATE_EMAIL))){
return "This email is valid.";}
else {
return "Please enter a valid email.";}
}
}
?>
HTML:
<div class="contactForm">
<label for="phone">Phone: </label>
<div id="errorphone" class="error"></div>
<input type="phone" name="phone" id="phone" placeholder="000-000-0000" required>
<p class="valid"><?= validatePhone($_POST['phone']);?></p>
</div>
<div class="contactForm">
<label for="email">Email*: </label>
<div id="erroremail" class="error"></div>
<input type="email" name="email" id="email" placeholder="janedoe@email.com" required>
<p class="valid"><?= validateEmail($_POST['email']);?></p>
</div>
When I run it I get these error messages.
Can anyone give me some pointers on what is causing these errors, or a better method to achieve the validation?