-1

网站链接: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

4 回答 4

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(...);
}
于 2013-07-04T21:15:53.260 回答
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);
于 2013-07-04T21:12:19.493 回答
0

您可以通过此代码作为名称

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

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

于 2013-07-04T21:23:31.027 回答
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; 
} ?>

相同 if 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 (负责发送电子邮件),如果执行该 echo 语句,则邮件应该可以工作。:)

于 2013-07-04T21:29:47.613 回答