2015-10-06 80 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