如何保存评论中的电子邮件

时间:2018-07-01 08:53:43

标签: php html

源代码:

<?php
date_default_timezone_set('Europe/Paris');

$content = "";

if(file_exists("messages.inc.html"))
   $content = file_get_contents("messages.inc.html");

if(isset($_POST['post']))
{
  $time = date('Y-m-d H:i:s');

  $content .=  "\n<b>From:</b> " . htmlspecialchars($_POST['name']) . 
               " <b>eMail:</b> " . htmlspecialchars($_POST['email']) . 
               " <b>at:</b> $time<br><br>" .
  htmlspecialchars($_POST['text']) . <"hr">;

  file_put_contents("messages.inc.html", $content);
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Message schreiben</title>
</head>
<body>
    <h1>Messages</h1>
    <h2>Insert new Message:</h2>
    <form action="" method="post">
        <label>E-Mail: <br><input type="text" name="email"><br></label>
        <label> Name: <br><input type="text" name="name"><br></label>
        <label> Message: <br><textarea cols="45" rows="6" name="text">    
        </textarea><br></label>
        <input type="submit" name="post" value="post">
    </form>

<?php
    if (strlen($content))
       echo $content;
    else
     echo "<h2>No Messages availabe</h2>";
?>
 </body>
 </html>

当某人将其电子邮件写在评论中时,我不希望以后再将其显示在评论旁边。相反,我想将电子邮件保存到我的**messages.inc.html**文件中。

我该怎么办?谁能帮我,谢谢!

1 个答案:

答案 0 :(得分:0)

人们帮助了我,有了新的《守则》,我可以做到。...

<!dictop html>
<html>
<body>

<form action="" method="post">
        <label>E-Mail: *<br><input placeholder="my e-mail" type="text" name="email"><br></label>
     <br>
        <label> Name: *<br><input placeholder="my name" type="text" name="name">  

  <br></label>
     <br>
        <label> Message: <br><textarea cols="45" rows="6" name="text"></textarea>

  <br></label>
     <p>
        <input  type="submit" name="post" value="POST COMMENT" id="comment">
     </p>
     <br>
     <h1>Comments</h1>
     <br>


</form>



<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message =  $_POST['text'];
$file = "test.txt";

if(isset($name) && isset($email) && isset($message)){
    $data = array("name" => $name, "email" => $email, "message" => $message);
    $data = serialize($data);
    file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX);
}

$messages = file($file);

foreach ($messages as $line) {
    $data = unserialize($line);
    echo "<b>" . "From:" . "</b>" . " " . $data["name"] . " ";
    echo "<b>" . "at:" . "</b>" . " " . $date = date('m/d/Y h:i:s a', time()) . "<br><br>";
    echo $data["message"] . "<br><br>";
    echo "<hr>";

}
?>

</body>
</html>

现在,我有我要的方式:

发件人:发件人: 2018年7月7日上午11:33:43

电子邮件不会与注释一起显示,而是要保存在test.txt文件中...。