0

这是我的工作代码,没有 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>
4

3 回答 3

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>
于 2013-02-26T11:41:17.357 回答
0

对于验证,您可以使用 jQuery 之类的

    $("#sendMessage").click(function(e){
    e.preventDefault();
    if($("#senderEmail").val()=="") {
     alert('Email Blank');
     return false;
    } else {
     return true;
    }
  });

使用 PHP 在 div 中显示消息

if ($mail_status){
$message = "Thanks for your mail...";
}else{
$message = "Please correct your data.";
}

现在你可以在任何你想要的 div中echo取出$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>
于 2013-02-26T11:49:40.277 回答
0

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>
于 2013-02-26T11:54:12.403 回答