2013-10-30 84 views
0

我有一個自己製作的消息系統(它不好),我需要一些幫助。我將一些文字輸入一個框然後提交。使用php刪除div

<form name="input" action="messagesave.php" method="POST">

$myFile = "messages.txt"; 
$fp = fopen($myFile, 'a') or die("can't open file"); 
$stringData = "<div class='messages'>" . $_POST["comment"] . "</div>" . "<br />"; 
fwrite($fp, $stringData); 
fclose($fp); 

這不僅節省了我的消息,一個單獨的頁面上此顯示我的消息

<?php include 'messages.txt';?> 

這頁我希望能夠刪除郵件時,我嘗試使用Javascript和人們告訴我你需要使用PHP來刪除它。我怎樣才能點擊郵件旁邊的內容來刪除它?

OR

是否可以保存該消息轉換成一個不同的文件,比如$ MYFILE每次= 「消息[1然後2然後3等..]」; 然後打開所有的PHP包括「消息[個個]的.txt」

OR

任何其他更好的方法來做到哪裏的註釋出現在另一頁上評論系統?

+1

有可能超過該文件一個消息?而不是你將如何刪除這個問題。 –

+0

如何刪除div信息 – user2092317

+0

這將有助於提及[你打開的文件裏面有什麼](http://stackoverflow.com/questions/19676455/delete-an-entire-div)。 – Nightfirecat

回答

0

,可以通過JavaScript只

例如

<div id="message"> 
<?php include 'messages.txt';?> 
</div> 

而且使用jQuery做刪除特定消息的內部格在例如這個文本文件 ,假設你的mesage會像

<div id="submessage">Some Text Here</div> 

$('#message').click(function(){ 
    this.find('#submessage').fadeOut();//This will remove comment div when clicking it's parent 
}); 
+0

我認爲他在那個txt文件裏面問div。 –

0

您可以使用unlink刪除整個文件?

http://php.net/manual/en/function.unlink.php

if ($_POST['delete_file]) { 
    unlink('message.txt'); 
} 

或者,如果你知道是什麼行的消息是在該文件中,你可以刪除了這一行;

+0

我怎麼能刪除每一行上的東西,因爲我在每一行的消息裏面有每個div – Friedpanseller

0

你需要的是提供的ID給每個DIV

$myFile = "messages.txt"; 

$todo = getRequest('todo', true); 

switch($todo) { 
    case 'add' : 
     $message = getRequest('msg', true); 
     if(is_null($message)) { 
      //hlandle it 
      echo "no message found to add!"; 
     } 
     addMessage($message); 
     break; 
    case 'remove': 
      $id = getRequest($id, true); 
      break; 
     default: 
     echo 'what do you want me to do?'; 
    } 

    function getRequest($key, $only_post = false) { 

     if($only_post) { 
      if(! isset($_POST[$key])) { 
       return null; 
      } 
      return $_POST[$key]; 
     } 
     if(! isset($_REQUEST[$key])) { 
      return null; 
     } 
     return $_REQUEST[$key]; 
    } 

    function addMessage($message) { 
     global $myFile; 
     $id = microtime(); 
     $div = "<div class=\"messages\" id=\"{$id}\">{$message}</div>"; 
     file_put_contents($myFile, $div ,FILE_APPEND); 
    } 

    function removeMessage($id) { 
     global $myFile; 
     $content_list = explode("\n" , file_get_contents($myFile)); 

     if(empty($content_list)) { 
      return false; 
     } 

     for($i = 0; $i < count($content_list); $i++) { 
      if(preg_match("/<div[^<>]*id=\"{$id}\"/i", $content_list[$i])) { 
       unset($content_list[$i]); 
       break; 
      } 
     } 
     file_put_contents($myFile, implode("\n", $content_list)); 
    }