-3

I know that there is a ton of solutions to this problem but i need to validate email in a contact form.

this is my php mailer so far:

<?php if (isset($_POST['send'])){ $to = "[email protected]"; $subject = "new message"; $firstname = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if($sent) {echo "<script>alert('thanks for the message:) ');</script>"; }else {echo "<script>alert('sorry, message wasn't send');</script>"; } } ?> 

I have tried with filter_var

$result = filter_var( '[email protected]', FILTER_VALIDATE_EMAIL ); 

but, doesn't work.

If my way to sent mail through contact form isn't the "best practice way" feel free to correct me and send me on the right path :D

9
  • possible duplicate of Validate Email in php Commented Jan 26, 2015 at 5:17
  • 2
    Why does filter_var not work? Commented Jan 26, 2015 at 5:17
  • put error_reporting(E_ALL); and check if there;s any error Commented Jan 26, 2015 at 5:18
  • I'll try :) Luke, i know but i have tried some different approaches and it still doesn't work, thats why i am asking. Mischa, yea :D i would like to know aswell... Commented Jan 26, 2015 at 5:22
  • 1
    "but, doesn't work." tells us nothing. Enable error reporting and tell us the error code Commented Jan 26, 2015 at 5:22

1 Answer 1

0

If you show us your HTML we can help further, but with just your PHP we don't have much to work with.

Add error_reporting(E_ALL); and ini_set('display_errors', 1); to the top of your php page to print your errors.

I am not sure what is causing your errors. But i would like you to try the following code. Replace your if statement with this.

Future note, to add a javascript alert in php you should format it this way,

if(mail($to, $subject, $message, $headers)) { echo '<script language="javascript">'; //echo open tag echo 'alert("Your message has been sent")'; // echo alert echo '</script>'; //echo close tag }else{ echo '<script language="javascript">'; echo 'alert("Your message was not successful")'; echo '</script>'; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Your welcome, try replacing your whole if statement and mail send to the bottom php code on this answer. If you have any errors please post back.
If this doesnt work for your needs, check out the top answer here stackoverflow.com/questions/5855811/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.