Php放内容:数据被删除

时间:2015-10-06 15:03:10

标签: 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>    

此脚本中的问题是第3个表单操作未执行。我得到了成功的消息,但是没有写出style.css文件&amp;它还会删除任何现有内容。前两个表单操作完全正常。

它不能是目录权限错误,因为其他2个文件正在运行。

2 个答案:

答案 0 :(得分: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>

在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 :(得分: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名称&amp; id应该是一样的。应在put_content $ _POST

上输入相同的名称