我只是想知道如果这个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";
?>
答案 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)
很多问题
代码:
(注意:这将全部放入文件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>