2017-07-15 38 views
0

我做了這個網站,http://mihaialin793.esy.es/new-homepage/portofolio/physics/,它有一個評論表。問題是,當我按提交時,在11條評論之後,HTML變得混亂,最後的評論被隱藏或刪除,我無法弄清楚。經過11條評論,PHP不斷丟失HTML標籤

PHP代碼:

<?php 
    if($_POST['submit']){ 

     print "<h1>Your comment has been sent!</h1>"; 

     $name = $_POST['name']; 
     $comment = $_POST['message']; 

     #Get old comments 
     $old = fopen("comment.txt", "r+t"); 
     $old_comments = fread($old, 1024); 

     #Delete everything, write down new and old comments 
     $write = fopen("comment.txt", "w+"); 
     $string = "<div class='comment'><span class='name'>".$name.":</span><span class='comm'>".$comment."</span></div><br>\n".$old_comments; 
     fwrite($write, $string); 
     fclose($write); 
     fclose($old); 
     header('Location: index.php'); 
    } 

    #Read comments 
    $read = fopen("comment.txt", "r+t"); 
    echo "<h2 class='other_comm'>Other comments</h2>".fread($read, 1024); 
    fclose($read); 
?> 

的HTML是那裏的網站上,但我會在這裏讓他們太:

HTML:

<form action="" method="post"> 
    <h2>Post your comment</h2> 
    <input type="text" name="name" placeholder="Name" required><br /> 
    <textarea name="message" placeholder="Comment"></textarea><br /> 
    <input type="submit" name="submit" value="Post"> 
</form> 

回答

0

的 'FREAD' 方法僅輸出1024字節在你的情況。

而是嘗試使用:

echo "<h2 class='other_comm'>Other comments</h2>".fread($read, filesize('comment.txt')); 
+0

謝謝,我知道我在PHP中的小白。 –

+0

不用擔心。它可以免費學習:-)。祝你的項目好運! – robinl