如何使用PHP验证此电子邮件联系表单?

时间:2013-07-04 21:07:15

标签: php html forms email

网站链接:http://www.leonardpfautsch.com/contact.php

如何仅使用PHP验证联系表单?我希望能够在有错误的文本字段下直接显示错误消息。对于每个文本字段,我不希望一次显示多个错误。如果您在字段中提交没有任何内容的表单,您会看到在名称和电子邮件下,每个表单都会显示两个错误。由于某些类型的规范,我希望错误只出现一次。现在我觉得我走在正确的轨道上。但是,下面的代码没有发送电子邮件。我是PHP的新手。如果有人能帮助我,我会非常感激。

<?php

if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))){


    $errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);

    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];


if ($name === '') {
    $name_error_1 = '<div style="color:red;"> Name is a required field! </div>';
}

if ($email === '') {
    $email_error_1 = '<div style="color:red;"> Email is a required field! </div>';
}

if ($subject === '') {
    $subject_error = '<div style="color:red;"> Subject is a required field! </div>';
}

if ($message === '') {
    $message_error = '<div style="color:red;"> Message is a required field! </div>';
}

if (isset($email) && (filter_var($email, FILTER_VALIDATE_EMAIL) === false)){
    $email_error_2 = '<div style="color:red;"> The email address must be real! </div>';
}

if (ctype_alpha($name) === false) {
    $name_error_2 = '<div style="color:red;"> Your name must only contain letters! </div>';
}

    /*Main way that mail works*/
    if (empty($errors) === true) {
        /*Where_mail_goes_to, Subject, Body_text, Who_email_is_from*/
        mail('email_address', $subject, "From " . $name . "\r\r" .  $message, 'From: ' . $email);
        /*Shows up in the URL if the message has been sent*/
        header('Location: contact.php?sent');
        exit();
    }

} //end of main if

?>


<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
                    <span class="label">Name</span><br/>
                    <?php if (isset($name_error_1)) { echo $name_error_1; } ?>
                    <?php if (isset($name_error_2)) { echo $name_error_2; } ?>
                    <input type="text" class="textfield" name="name" size="50" maxlength="50" <?php if (isset($_POST['name']) === true) { echo 'value="', strip_tags($_POST['name']), '"'; } ?> > <br/>
                    <span class="label">Email</span><br/>
                    <?php if (isset($email_error_1)) { echo $email_error_1; } ?>
                    <?php if (isset($email_error_2)) { echo $email_error_2; } ?>
                    <input type="text" class="textfield" name="email" size="50" maxlength="50" <?php if (isset($_POST['email']) === true) { echo 'value="', strip_tags($_POST['email']), '"'; } ?> > <br/>
                    <span class="label">Subject</span><br/>
                    <?php if (isset($subject_error)) { echo $subject_error; } ?>
                    <input type="text" class="textfield" name="subject" size="50" maxlength="50" <?php if (isset($_POST['subject']) === true) { echo 'value="', strip_tags($_POST['subject']), '"'; } ?> > <br/>
                    <span class="label">Message</span><br/>
                    <?php if (isset($message_error)) { echo $message_error; } ?>
                    <textarea rows="5" cols="50" name="message" id="textarea" maxlength="500"><?php if (isset($_POST['message']) === true){ echo $_POST['message'];}?></textarea><br/>
                    <input type="submit" value="Send" id="submit" name="action"> 
                    </form>

4 个答案:

答案 0 :(得分:0)

您可以使用elseif检查

来执行此操作
<span class="label">Email</span><br/>
<?php if (isset($email_error_1)) 
{ 
echo $email_error_1; 
}
elseif(isset($email_error_2)) {
 echo $email_error_2; 
} ?>

在最后一次验证检查后也移动此行

if (ctype_alpha($name) === false) {
    $name_error_2 = '<div style="color:red;"> Your name must only contain letters! </div>';

}
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);

答案 1 :(得分:0)

您可以为每个字段创建一个错误数组,并仅显示添加到其中的第一个错误。

<?php
$email_errors = array();

if ($email == '')
{
    $email_errors[] = 'First error';
}

if (more_email_checks($email) == false)
{
    $email_errors[] = 'Second error';
}
?>

...

<span class="label">Email</span><br />
<?php echo array_shift($email_errors); ?>

要知道是否发送电子邮件,您可以这样做:

$errors_found = 0;

if (check_email($email) == false)
{
    $email_error = 'Error message';
    $errors_found++;
}

...

if ($errors_found == 0)
{
     mail(...);
}

答案 2 :(得分:0)

您可以通过此代码获取名称

<?
$message = "<div style = 'color :red ' />  ; 
if (isset(name == '' ) {
 echo $message 
}
?>

这是名称php vaildation,但你可以创建js

答案 3 :(得分:0)

将您的电子邮件验证更改为:

    <span class="label">Email</span><br/>
<?php if (isset($email_error_1)) 
{ 
echo $email_error_1; 
}else if(isset($email_error_2)) {
 echo $email_error_2; 
} ?>

如果else可以应用于具有多个验证条件的所有字段,则相同。

然后将错误数组移到电子邮件条件检查上方:

<?php
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);

//and change your mail function as:


    $to      = 'email_address';
    $headers = $headers .= 'From: $name <$email>';

    mail($to, $subject, $message, $headers);

    ?>

同时对控件进行检查,如果你已完全填写表格,那么它应该来邮件功能,我的意思就是检查if条件,万一你遇到条件问题,试着回音一下在if语句中(负责发送电子邮件),如果执行了echo语句,那么邮件应该可以工作。 :)