-3

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 . "\r\n" .
   'Reply-To: ' . $email . "\r\n" .
   '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!

ck777
  • 161
  • 1
  • 1
  • 20
  • That is a very broad question and not as specific as mine. I am asking how to do this with my particular contact form. This person hasn't even put their form code. – ck777 May 17 '17 at 14:55
  • 1
    @DouwedeHaan indeed, this could help him. @ck777 you could also you the input type `email` that require a @ in the text input. – Zooly May 17 '17 at 14:55
  • @ck777: `"I am asking how to do this with my particular contact form."` - And when you try to implement that solution in your form, what doesn't work? You're *very unlikely* to find another question which produces *your exact code*. However, as other questions cover the concepts and implementations, you can use that knowledge to build your own form. – David May 17 '17 at 14:57
  • @Zooly this is exactly what I would like to do but I am a little confused as to how - I have obviously searched lots but perhaps am using the wrong search terms. – ck777 May 17 '17 at 14:59
  • @David When I implement the solution I have tried in my form, it still allows me to enter an invalid email address, as I mentioned above. The question someone has said this is a duplicate of just asked if one line of code was okay to validate an email address on a form. I am not sure my very long worded question is a "duplicate" – ck777 May 17 '17 at 15:01
  • 1
    Check this link to read more about `type="email"` in HTML. But it won't solve 100% your demand – Zooly May 17 '17 at 15:01
  • 1
    @zooly Ah no I understand what you mean completely sorry!! I thought I had this already so I will give it a go, I may have changed it when editing the form sometime which may explain why it no longer works..Thank you! – ck777 May 17 '17 at 15:03
  • 1
    type value should be email for email field and required attribute will check whether your given mail is valid email or not. – Pritam Karmakar May 17 '17 at 15:04
  • It is all fixed! It was the type="email" - I must have accidentally changed this sometime and hence it had stopped validating email addresses! – ck777 May 17 '17 at 15:06
  • @PritamKarmakar thank you it was indeed this! – ck777 May 17 '17 at 15:10

3 Answers3

3

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.\n";
} else {
    echo "This ($email_b) email address is considered invalid.\n";
}
Gabriel Heming
  • 1,100
  • 10
  • 30
  • But would this check the email addresses users input are valid? would this not just check the email the form is going to is valid? Sorry for my confusion – ck777 May 17 '17 at 14:57
  • It is server side checking whether the email format given is correct or not@ck777 – Pritam Karmakar May 17 '17 at 15:22
2

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"/>
Amir Shahbabaie
  • 1,352
  • 2
  • 14
  • 33
  • Yes this is exactly what I want, thank you! So litteraly just that? Amazing thank you I will try that, I thought I had that already so perhaps a typo on my part..! – ck777 May 17 '17 at 15:02
  • I appreciate if you vote my answer – Amir Shahbabaie May 17 '17 at 15:04
  • Note: Users *can* still send invalid data. Browsers which don't support this will likely treat this as any normal text input, and in browsers which do support this users can still bypass it. This will work in most cases, but if a user *wants* to submit invalid data, they still can. – David May 17 '17 at 15:05
  • Hey, thanks a lot it was indeed this, or at least this is how I had it before when I assumed it was working. Not sure how this happened but thanks a lot for pointing it out – ck777 May 17 '17 at 15:07
-2

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

  • I would really just like to make my current form do it as I made this form exactly how I want it.. I have added some code to make this work, please see my code above – ck777 May 17 '17 at 14:56
  • Don't rely on a link to be your answer, add the information to your answer. – colonelclick May 17 '17 at 14:58