PHP联系表单验证

时间:2013-04-22 11:07:16

标签: php html forms validation

我只是想知道如果这个PHP代码可用于验证HTML联系表单输入,是否有人可以请求帮助。我按照教程创建了这个PHP验证,但我不确定它是否可行。我还没有测试它的虚拟主机,但如果有人有服务器,如果有人能帮我一个忙,我将非常感激。如果您可以发送/接收电子邮件,请尝试代码。谢谢!! 我正在使用jQuery Validation Plugin在客户端验证表单,这是教程http://www.youtube.com/watch?v=rdsz9Ie6h7I
 HTML表单:

<form action="contact.php" method="post">
<label for="yourname">Your Name:</label>
<input type="text" name="YourName"/>

<label for="youremail">Your Email:</label>
<input type="text" name="YourEmail" />

<label for="yourmessage">Your Message:</label>
<textarea  name="YourMessage"></textarea>

<fieldset>
<input type="submit" id="submit" value="Send"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>

PHP Code:

<?php

/* Subject and Email Variables */

 $emailSubject = 'Email from site visitor';
 $webMaster = 'YourEmail@mail.com';

/* Getting Form Data Variables */

    $nameField = $_POST['YourName'];
    $emailField = $_POST['YourEmail'];
    $messageField = $_POST['YourMessage'];

    $body = <<<EOD
<br><hr><br>
Name: $YourName <br>
Email: $YourEmail <br>
Message: $YourMessage <br>

EOD;

    $headers = "From: $YourEmail\r\n";  
    $headers .= "Content-type: text/html\r\n";

    $success = mail($webMaster, $emailSubject, $body, $headers);

    $theResults = <<<EOD
<html>
<head>
</head>
<body>
<p style="font-size:12px;font-family:Tahoma,Verdana;">Thanks for your Message.</p>
</body>
</html>

EOD;

echo "$theResults";

?>

2 个答案:

答案 0 :(得分:0)

如果您现在没有网络服务器,可以安装本地服务器并在那里进行测试。完全免费。

http://www.apachefriends.org/en/xampp-windows.html#641

/* Getting Form Data Variables */

    $nameField = $_POST['YourName'];
    $emailField = $_POST['YourEmail'];
    $messageField = $_POST['YourMessage'];

    $body = <<<EOD
<br><hr><br>
Name: $nameField<br>
Email: $emailField <br>
Message: $messageField <br>

答案 1 :(得分:0)

很多问题

  1. 你在PHP方面根本没有验证。我在
  2. 下面添加了成功或失败消息的验证和条件呈现
  3. 您没有使用检索到的变量名称(而是使用$ YourEmail) - 帖子名称
  4. 您的表单标签与输入名称不匹配
  5. 您的表单元素未关闭,并且您的字段不在字段集中,并且字段集没有图例。
  6. 如果失败,我在表单中添加了一个值预填充,因此当提交时,人们不会返回空白表单。
  7. 为此,我将表格放在PHP
  8. 之后

    代码:

    (注意:这将全部放入文件contact.php

    <?php
    
    if (isset($_POST['submit'])) {
    
        /* Getting Form Data Variables */
    
        $nameField = isset($_POST['YourName']) ? $_POST['YourName'] : null;
        $emailField = isset($_POST['YourEmail']) ? $_POST['YourEmail'] : null;
        $messageField = isset($_POST['YourMessage']) ? $_POST['YourMessage'] : null;
    
        // Validate
        $failures = array();
        if (strlen($nameField)) $failures[] = "Name is required";
        if (strlen($emailField)) $failures[] = "Email is required";
        if (filter_var($email,FILTER_VALIDATE_EMAIL) === false) $failures[] = "Email is invalid";
        if (strlen($messageField)) $failures[] = "Message is required";
    
        // If validation errors, render them
        if (count($failures)) {
            echo "<p><b>Failed to submit: " . implode(", ", $failures) . "</b></p>";
        } else {
            /* Subject and Email Variables */
            $emailSubject = 'Email from site visitor';
            $webMaster = 'YourEmail@mail.com';
    
            $body = <<<EOD
                <br><hr><br>
                Name: {$nameField} <br>
                Email: {$emailField} <br>
                Message: {$messageField} <br>
            EOD;
    
    
            $headers = "From: {$emailField}\r\n";  
            $headers .= "Content-type: text/html\r\n";
            $success = mail($webMaster, $emailSubject, $body, $headers);
    
            $theResults = <<<EOD
                <p>Thanks for your Message.</p>
    EOD;
    
            echo "$theResults";
        }
    }
    
    ?>
    
    <form action="contact.php" method="post">
        <fieldset>
            <legend>Contact Us</legend>
            <label for="YourName">Your Name:</label>
            <input type="text" name="YourName" value="<?=$nameField?>" />
    
            <label for="YourEmail">Your Email:</label>
            <input type="text" name="YourEmail" value="<?=$emailField?>" />
    
            <label for="YourMessage">Your Message:</label>
            <textarea  name="YourMessage"><?=$messageField?></textarea>
    
            <input type="submit" id="submit" value="Send"/>
            <input type="reset" id="reset" value="Reset"/>
        </fieldset>
    </form>