在HTML页面的TextArea中输入的文本未保存在文本文件中。

时间:2013-11-09 15:25:00

标签: php html

我正在尝试将以HTML格式输入的数据保存在文本文件中。我使用PHP脚本来做到这一点。 当我单击提交按钮时,它不会将数据保存在文本文件中。 谁能告诉我这里出了什么问题。

以下是代码段 -

HTML表单 -

<form id="post" name="post" method="post" action="input.php">
    Name: <input type="text" name="name"><br>
    Text: <textarea rows="50" cols="85" name="blogentry"></textarea>
    <input class="button" type="submit" value="Submit">
</form>

PHP - (input.php)

<html>
   <head></head>
   <body>
   <?php 

   // variables from the form
   $name = $_POST['name'];
   $blogentry = $_POST['blogentry'];

    // creating or opening the file in append mode
    $dataFile = "data.txt";
    $fh = fopen($dataFile, 'a');

    // writing to the file
    fwrite($fh, "Name - " . " " . $name . " " . "\n");
    fwrite($fh, "Blog - " . " " . $blogentry . " " . "\n\n");

    fclose($fh);
    ?>
   </body>
</html>

4 个答案:

答案 0 :(得分:0)

在这种情况下,确定问题是有用的。也许表单没有提交你的textarea,或者你的PHP没有收到它,或者这可能与你正在使用的价值有关。

如果您在Firebug等检查员中查看表单提交,您是否在请求中看到了textarea的内容?

如果您在代码中执行var_dump($_POST),是否看到表单中提交的所有值?

答案 1 :(得分:0)

你能试试吗,

   if(isset($_POST['name']) && isset($_POST['blogentry'])){

            $name = $_POST['name'];
            $blogentry = $_POST['blogentry'];

            // creating or opening the file in append mode
            $dataFile = "data.txt"; // make sure the directory path is correct and permission of the folder
            $fh = fopen($dataFile, 'w');    // writing to the file

            $stringData = "Name - " . " " . $name . " " . "\n";
            $stringDataBlog = "Blog - " . " " . $blogentry . " " . "\n\n";
            fwrite($fh, $stringData);                  
            fwrite($fh, $stringDataBlog);  
            fclose($fh);                
  }

答案 2 :(得分:0)

您的代码没有错误。如果data.txt在那里并且具有在其上写入的权限,则代码应该起作用。请检查文件权限。

答案 3 :(得分:0)

我遇到了同样的问题。您需要将带有form id的form标签添加到textarea元素中。纠正上面的代码::

<form id="post" name="post" method="post" action="input.php">
    Name: <input type="text" name="name"><br>
    Text: <textarea rows="50" cols="85" name="blogentry" form="post"></textarea>
    <input class="button" type="submit" value="Submit">
</form>

然后它应该工作。