2015-10-06 83 views
-1

我有這個簡單的網頁內容編輯器。PHP內容:刪除數據

<?php 
    if (isset($_POST['edit'])) { 
     if (file_put_contents('homecontent.txt', $_POST['homecontent']) !== FALSE) 
      echo '<p class="success">Home Content Saved Successfully!</p>', "\n"; 
    } 
    $homecontent = file_get_contents('homecontent.txt'); 

    if (isset($_POST['header'])) { 
     if (file_put_contents('headercontent.txt', $_POST['headercontent']) !== FALSE) 
      echo '<p class="success">Header Content Saved Successfully!</p>', "\n"; 
    } 
    $headercontent = file_get_contents('headercontent.txt'); 

    if (isset($_POST['cssfile'])) { 
     if (file_put_contents('style.css', $_POST['cssfile']) !== FALSE) 
      echo '<p class="success>Style.css Saved Successfully!</p>', "\n"; 
    } 
    $cssfile = file_get_contents('style.css'); 
?> 

<div id="forms"> 

<form method="post" action=""> 
    <p>Here you can edit your homepage text:</p> 
    <textarea name="homecontent" id="homecontent" rows="20"><?php echo $homecontent?></textarea> 
    <p><buton type="submit" name="edit">Save changes</button></p> 
</form> 


<form method="post" action=""> 
    <p>Here you can edit your header content:</p> 
    <textarea name="headercontent" id="headercontent" rows="10"><?php echo $headercontent?></textarea> 
    <p><button type="submit" name="header">Save changes</button></p> 
</form> </div> 

<form method="post" action=""> 
    <p>Here you can edit style.css file:</p> 
    <textarea name="cssfile" id="cssfile" rows="10"><?php echo $css?></textarea> 
    <p><button type="submit" name="cssfile">Save changes</button></p> 
</form>  

這個腳本中的問題是第三個表單動作沒有得到執行。我得到成功消息,但style.css文件沒有寫入&它也刪除任何現有的內容。前2個表單動作完美無缺。

因爲其他2文件正在工作,所以不能是目錄權限錯誤。

+0

$ _POST ['cssfile']中的某些示例內容是什麼?作爲一個測試,試試file_put_contents('style.css','hello world')!== FALSE ...如果這樣的話,你知道這個問題與$ _POST ['cssfile'] –

+3

錯誤變量的內容有關。錯誤報告會告訴你。另外,一個錯字'

+0

我也看到了錯字,但假定它在原始源代碼中沒問題,因爲否則就沒有提交按鈕會立即啓動。 – syck

回答

2

您的提交按鈕名稱與textarea相同,因此它會覆蓋它。形式更改爲

<form method="post" action=""> 
    <p>Here you can edit style.css file:</p> 
    <textarea name="cssfile" id="cssfilecontent" rows="10"><?php echo $cssfile; ?></textarea> 
    <p><button type="submit" name="cssfile">Save changes</button></p> 
</form> 

Up在PHP部分,你將不得不改變

if (isset($_POST['cssfile'])) { 
     if (file_put_contents('style.css', $_POST['cssfilecontent']) !== FALSE) 
      echo '<p class="success>Style.css Saved Successfully!</p>', "\n"; 
    } 
    $cssfile = file_get_contents('style.css'); 

,當然,<p><buton type="submit"在第一種形式錯過了t。我猜想,在第二種形式之後的</div>應該在第三種之後。

+1

也''cssfile = file_get_contents('style.css');'和echo $ ing'$ css' –

+0

爲什麼要命名一個提交按鈕? – barfoos

+0

@AlexTartan我默默地改變了這一點。 – syck

0

感謝您的所有建議。我終於得到它的工作

if (isset($_POST['cssfile'])) 
     { 

      if (file_put_contents('style.txt', $_POST['csscontent']) !== FALSE)   echo '<p class="success">Style.css Saved Successfully!</p>', "\n"; 
     } 

     $csscontent = file_get_contents('style.txt'); 


     ?> 

    <html><body> 

    <form method="post" action=""> 
     <p>Here you can edit style.css file:</p> 
     <textarea name="csscontent" id="csscontent" rows="10"><?php echo $csscontent ?></textarea> 
     <p><button type="submit" name="cssfile">Save changes</button></p> 
    </form> 
    </body> 
    </html> 

textarea name & id應該是相同的。應在put_content上輸入相同的名稱$ _POST