这是我的工作代码,没有最新的Boynux建议。现在我将尝试将两者结合起来,如果发生错误,可能会获得红色div框。感谢所有人的帮助,我认为现在我非常接近我正在考虑的结果。
PHP
<section id="main-content">
<div class="text-intro"><h2>KONTAKT</h2></div>
<?php
if(isset($_POST['sendMessage'])) {
$to = "info@test.com";
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'E-mail: '.$email."\n";
$from = 'Message: '.$message;
$body_message = "Question";
echo "<div id=\"submitmessage\">Thank you.</div>";
mail($to, $body, $from);
} else {
echo "Please check you data.";
}
?>
<div class="columns features">
<br />
<form action="contact.php" method="post" id="contact-form" class="contact-form">
<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>
HTML
<section id="home-head" class="contact">
<h1></h1>
</section>
<section id="main-content">
<div class="text-intro"><h2>CONTACT</h2></div>
<div class="columns features">
<br />
<form action="contact.php" method="post" id="contact-form" class="contact-form">
<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>
</section>
答案 0 :(得分:0)
<section 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">
<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>
if($("#senderEmail").val()==''){
// alert the message
return false;
}
</script>
答案 1 :(得分:0)
对于验证,您可以使用jQuery,如
$("#sendMessage").click(function(e){
e.preventDefault();
if($("#senderEmail").val()=="") {
alert('Email Blank');
return false;
} else {
return true;
}
});
使用PHP
在消息中显示消息if ($mail_status){
$message = "Thanks for your mail...";
}else{
$message = "Please correct your data.";
}
现在,您可以echo
在您想要的任何div中$message
<div class="message_area"><?php echo $message;?></div>
更新
<section id="main-content">
<div class="text-intro"><h2>KONTAKT</h2></div>
<?php
if(isset($_POST['sendMessage'])) {
$to = "info@test.com";
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'E-mail: '.$email."\n";
$from = 'Message: '.$message;
$body_message = "Question";
echo "<div id=\"submitmessage\">Thank you.</div>";
mail($to, $body, $from);
} else {
$message = "Please check you data.";
}
?>
<div class="columns features">
<br />
<form action="contact.php" method="post" id="contact-form" class="contact-form">
<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>
<div class="message_div">
<?php
if(isset($_POST['sendMessage'])) {
if(isset($message)) {
echo $message;
}
}
?>
</div>
<br /><br /><br /><br /><br />
</div>
答案 2 :(得分:0)
PHP不像ASP.NET或simillar框架,其中模板和代码是分开的。如果您不考虑其他框架,如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>