如何使我当前的PHP联系表单检查电子邮件地址输入是否有效? [重复]

如何使我当前的PHP联系表单检查电子邮件地址输入是否有效?  [重复]

问题描述:

This question already has an answer here:

Fixed:

There was a small error in my code, I had changed the "type" to "text" somehow in the email input, where as it should have been

type="email" 

and not

type="text"

I have made a php contact form which I use on website I make and all works fine.

I thought the form was checking the email address had been entered but I have now realised people can just put their name in the email input and not their full email address (i.e with an @ symbol and a .com or .co.uk at the end)

I am not so great at PHP so I have had a go but emails from the form where I haven't put a valid email address in still seem to go through despite the statements I added

I tried to define some error variables and set with empty values as follows:

$nameErr = $emailErr = $genderErr = $websiteErr = "";

and then adding these if else statements to my form php:

if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}

So the my code looks as follows:

<?php 

    $name = ($_POST['name']);
    $email = ($_POST['email']);
    $message = ($_POST['message']);
    $from = ($_POST['email']);
    $to = 'me@myemailaddress.co.uk'; 
    $subject = "Enquiry from Visitor " . $name;
    $human = ($_POST['human']);

   $headers = 'From: ' . $email . "
" .
   'Reply-To: ' . $email . "
" .
   'X-Mailer: PHP/' . phpversion();


?>

<div class="container-fluid">
<div class="about-msg">
<h2>CONTACT US</h2>

<div class="container-fluid contactform">
<div class="col-md-7 contactform-padding">
<br>    
<h1>GET IN TOUCH</h1>
<p>Please drop us a message if you have any enquires. We always aim to reply 
within 24 hours.</p>
<?php 
    if (isset($_POST['submit']) && $human == '4') { 
    if (mail ($to, $subject, $message, $headers)) { 
    if (empty($_POST["email"])) {               
       $emailErr = "Email is required";
       } else {
     $email = test_input($_POST["email"]);
     }
    echo '<p>Thanks for getting in touch. Your message has been sent & we 
will get back to you shortly!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
} 
    } else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
    } 
?>
<form method="post" action="contact-us.php" data-ajax="false" method="POST" 
enctype="multipart/form-data">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label " for="name">
<b>NAME</b>
</label>
<input class="form-control" id="name" name="name" type="text" 
placeholder="name">
</div>


<div class="form-group col-md-6">
<label class="control-label requiredField" for="email">
<b>EMAIL</b>
<span class="asteriskField">
*
</span>
</label>
<input class="form-control" id="email" name="email" placeholder="email" 
type="text"/>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="control-label requiredField" for="contact-subject">
<b>SUBJECT</b></label>
<input type="text" name="subject" class="contact-subject form-control" 
id="contact-subject" placeholder="subject">
</div>
</div>


<div class="form-group">
<label class="control-label " for="message">
<b>MESSAGE</b>
</label>
<textarea class="form-control" cols="40" id="message" name="message" 
rows="10" style="height: 275px !important;"></textarea>
</div>
<div class="form-group">

<label><b>*What is 2+2? (Anti-spam)</b></label>
<input name="human" placeholder="Type Here"></div>
<br>
<button class="btn btn-success" name="submit" type="submit" value="submit">
SEND
</button>
</form>


</div>

However this lets me send an email despite putting my email address as just my name!

Could someone please take a look and see if I have put the code in the right place or make a suggestion as to how to fix it? I am just learning PHP so I may have made an error in my code or maybe need to take the if statement out for the submit?

Thanks for any help!

</div>

I think you want the user to enter a valid email address, right? You could change the type to email

<input class="form-control" id="email" name="email" placeholder="email" type="email"/>

Well you could either go for regex but this is also an option

Use PHP Filter Validation

$email_b = "me@myemailaddress.co.uk";

if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.
";
} else {
    echo "This ($email_b) email address is considered invalid.
";
}