PHP 不像 ASP.NET 或模板和代码分离的类似框架。如果您不考虑其他框架,例如 Symfony 或 CakePHP,您需要将完整的 HTML 输出打印到浏览器:
<?php
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'email@email.de ';
$subject = 'Message from a site visitor '.$field_name;
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
?>
<html>
<head>
<title>Email result</title>
</head>
<body>
<div>
<?php
if ($mail_status){
echo "Thanks for your mail...";
}else{
echo "Please correct your data.";
}
?>
</div>
</body>
对于表单验证,您可以使用 javasctipt:
<div id="main-content">
<div class="text-intro"><h2>CONTACT</h2></div>
<div id="thanks"></div>
<div class="columns features">
<br />
<form action="contact.php" method="post" id="contact-form" class="contact-form" onsubmit="return validate ()>
<textarea id='message' maxlength='10000' name='message' placeholder='Hello...'> </textarea>
<div class='crayonico'></div>
<input class='text-input' id='senderEmail' maxlength='50' name='email' placeholder='E-Mail' type='email'>
<div class='emailico'></div>
<input class='btn btn-input' id='sendMessage' name='sendMessage' type='submit' value='SEND'">
</form>
<br /><br /><br /><br /><br />
</div>
<script language="javascript">
function validate()
{
// for example I check sender email
if ( document.getElementById ( 'senderEmail' ).value.length > 0 )
return true;
else
alert ( "Sender address required" );
return false;
}
</script>
但请记住,使用 javascript 进行验证是不够的,因为用户可以简单地绕过此检查,操作系统考虑再次检查 PHP 代码中的所有内容。
好的,这是一个完整的示例,但请注意,这不是很好的编写和完整的代码,这只是一个示例:
<html>
<head>
<title>Email result</title>
</head>
<body>
<?php
// function to compose email
function sendMail ( $sender, $message ) {
$mail_to = 'email@email.de ';
$subject = 'Message from a site visitor '.$field_name;
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
return mail($mail_to, $subject, $body_message, $headers);
}
// check if form submited?
if ( isset ($_POST['sendMessage']) ) {
$email = trim ($_POST['email']);
$message = trim ($_POST['message']);
if ($email == null || strlen ($email) == 0 ) {
$alert = "Sender address required.";
} else if ( $message == null || strlen ($message) == 0 ) {
$alert = "Empty messages is not allowed.";
} else if ( sendMail ( $email, $message ) ) {
$alert = "Thanks for your mail...";
} else {
$alert = "Please correct your data.";
}
}
?>
<?php if ( isset ($alert) ): ?>
<div style='color: red; font-weight: bold;'><?php echo $alert ?></div>
<?php endif; ?>
<div id="main-content">
<div class="text-intro"><h2>CONTACT</h2></div>
<div id="thanks"></div>
<div class="columns features">
<br />
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="contact-form" class="contact-form" onsubmit="return validate ()">
<textarea id='message' maxlength='10000' name='message' placeholder='Hello...'></textarea>
<div class='crayonico'></div>
<input class='text-input' id='senderEmail' maxlength='50' name='email' placeholder='E-Mail' type='email'>
<div class='emailico'></div>
<input class='btn btn-input' id='sendMessage' name='sendMessage' type='submit' value='SEND'>
</form>
<br /><br /><br /><br /><br />
</div>
<script language="javascript">
function validate()
{
// for example I check sender email
if ( document.getElementById ( 'senderEmail' ).value.length > 0 )
return true;
else
alert ( "Sender address required" );
return false;
}
</script>
</body>
</html>