0

我正在尝试编写一个程序,其基本思想是要求用户在 textarea 中输入,然后将文本存储到 word 文件中。这是我尝试使用的代码:

<html>
<head>
<title>Simple Guestbook</title>
</head>

<body>
<h1>Simple Guestbook Comment Creator</h1>
<br>
<form method = "post"
        action = "mysite.php">
    <textarea name = "text"
        rows = "10"
        cols = "20">Write Here</textarea>

<input type = "submit"
        value = "Submit Comment">

</form>

<?
    if($_POST['text'] !== NULL){
        $comment = $_POST['text'];


    $file = fopen("texttest.txt", "a");
    fputs($file, "<br>\n$comment");
    fclose($file);  
    }       

?>

</body> 
</html>

我似乎无法让它正常工作。我也在考虑以某种方式让表单操作存储文本然后重新加载网站,但我还没有让它工作(原始文件是 mysite.php,所以操作只是重新加载页面)。

如果有人对要使用的算法/要使用的不同语法有任何更好的想法,请告诉我,因为我刚刚开始学习基本的 PHP 语法。

谢谢

4

2 回答 2

1

检查以下内容:

  1. php是否有权在该目录中写入文件?
  2. 那个php文件叫“myfile.php”吗?

无论如何,当某些东西不起作用并且您想知道导致错误的原因时,请将error_reporting(-1);其放在 php 的开头 - 它会输出任何错误或警告,包括由 fopen() 生成的错误或警告。

此外,您可能想检查变量是否已正确提交:echo $comment在您分配它之后。

于 2011-04-09T05:06:22.717 回答
0

这样的事情可能会奏效。

您可能想对他们输入的值做更多的事情,但这基本上会满足您的要求。

您还需要确保您拥有要写入的文件的正确路径,并且该文件具有允许将其写入的正确权限:

<html>
<head>
    <title>Simple Guestbook</title>
</head>

<body>
    <h1>Simple Guestbook Comment Creator</h1><br>

    <?php
        if (isset($_POST['submit'])) {
            if (strlen(trim($_POST['comment']))) {
                $file = fopen("texttest.txt", "a");
                fputs($file, "$_POST['comment'])\n");
                fclose($file);  
            }
        } else {
    ?>
    <form method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">

        <label>Leave your comment
        <textarea name="comment" rows="10" cols="20"></textarea>
        </label>

        <input type="submit" name="submit" value="Submit Comment" />

    </form>
    <?php
        }
    ?>
</body> 

此外,由于您要返回同一页面,因此您可能需要输入某种消息,让该人知道他们已成功在您的地址簿中输入了某些内容。

于 2011-04-09T14:03:36.663 回答