Question

Below is the code for a contact form validation script that I would like to extend.

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: DaVv'; 
    $to = 'mail@mail.com'; 
    $subject = 'The topic';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit'] {               
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
?>

I want to add one more message for user about email, something like: "Please enter valid email address." when, for example, they do not write "@". I know that this is included in html 5 but I'd like to make this w/o HTML5.

Was it helpful?

Solution 2

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: DaVv'; 
    $to = 'mail@mail.com'; 
    $subject = 'The topic';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if(isset($_POST['submit']))
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false)
        {               
            if(mail ($to, $subject, $body, $from))
                echo '<p>Your message has been sent!</p>';
            else
                echo '<p>Something went wrong, go back and try again!</p>'; 
        }
        else
            echo '<p>Wrong Email</p>';

    }
    else if(!isset($_POST['submit']))
        echo '<p>You answered the anti-spam question incorrectly!</p>';
?>

OTHER TIPS

I'm not really sure what you are trying to achieve but I'm assuming it's server side validation for an e-mailaddress?

If so you could use the following PHP snippet:

if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // The user supplied an invalid e-mailaddress
}

sorry guys i wrote wrong code previously (coz i was in work)

this is the correct:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: domain.com'; 
$to = 'dawid@main.com'; 
$subject = 'Message from domain.com';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
   if ($name != '' && $email != '') {                
      if (mail ($to, $subject, $body, $from)) { 
         echo '<p class="msg">Your message has been sent!</p>';
      } else { 
         echo '<p class="msg">Something went wrong, go back and try again!</p>'; 
      } 
             } else {
                 echo '<p class="msg">You need to fill in all required fields!!</p>';
             }
  }
?>       

And to this i would like to add email validation (SERVER SIDE) and msg "u wrote wrong email" or sth like that... but i dont wanna delete any of the messages which are included now in the contact form. Just wanna add info about wrong email.

Thanks for help once more ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top