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 コードですべてを再度チェックすることを検討してください。
OK、ここに完全なサンプルがありますが、これはよく書かれておらず、完全なコードではないことに注意してください。これは単なる開始用のサンプルです。
<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>