PHP email question

Hi,

I got an email contact form page on my website, with the following php code:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \
 Message: $message";
$recipient = "elvis@presley.com";
$subject = "Web Mail";
$mailheader = "From: $email \\r\
";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$url = 'http://www.trusa...html#!/page_More';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

Now, it works just fine when sending, but my situation is that apparently people can just press the Send button even if they don’t fill everything out.

Can anyone tell me what the code would be to have all fields required to be filled?

Thanks!

You need some form of validation, which can be done either client side ie JavaScript or server side such as PHP.

I am not an expert, but would be willing to share some code with you. You might also look-up JQuery validation for email forms.

Hi there,

What you’re talking about here is validation.

Say for example, you will needed to validate that all fields are filled in and that the email address is formatted correctly.

  • After you identify which fields are required, you can check that they are filled in with the empty() function.
  • You will also need to verify that the email address is formatted correctly.

Here’s what the solution might look like.


<?php 

// very basic validation function.
function checkForInvalidFields($data) {
    // create an array for invalid fields
    $invalidFields = array();

    // check if name field is empty.
    if(empty($data['name'])) {
        $invalidFields[] = 'name';
    }

    // check if email field is empty.
    if(empty($data['email'])) {
        $invalidFields[] = 'email';
    } else {
        // check that the email field is formatted correctly;
        if(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
            $invalidFields[] = 'email';
        }
    }

    // check if message field is empty.
    if(empty($data['message'])) {
       $invalidFields[] = 'message';
    }

    // return an array of invalid fields. 
    return $invalidFields;
}

// get a list of invalid fields.
$invalidFields = checkForInvalidFields($_POST);

// initialise the error variable. 
$error = '';

// check if there are invalid fields detected.
if(count($invalidFields) == 0) {
    $name = $_POST['name']; 
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent=" From: $name \
 Message: $message"; 
    $recipient = "elvis@presley.com"; 
    $subject = "Web Mail"; 
    $mailheader = "From: $email \\r\
"; 
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); 
    $url = 'http://www.trusa...html#!/page_More';
    //echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    header("Location: $url");
} else { 
    // create error message.
    $error = '<p>Please review the following fields for errors: </p>'; // or something similar
    $error .= '<ul>';
    foreach($invalidFields as $field) {
        $error .= '<li>'.$field.'</li>';
    }
    $error .= '</ul>';
}
// display error message
echo $error;
?>

Note that the validation function I cooked up is very basic, but hopefully it gives you the idea.

HTH.

Regards,
rvdavid