1

作成したフォームへのすべての応答の現在までの合計を保持しようとしていますが、各応答が改行されるようにするのに問題があります。私は自分のコードを下に持っています。読みやすくしたいだけです。現在、すべての応答が詰まっているため、それぞれを新しい行に配置したいからです。私はいくつかのことを試し、コードとその結果にコメントを付けました。前もって感謝します。

<?php
if (isset($_POST['sometext']))
    {
    $myFile = "testFile.txt";
    $thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0 
    writemyfile($myFile,$thetext,"a");
    } else
    {
    $thetext="Enter text here";
    }

function readmyfile($thefile)
    {  
        $file = fopen($thefile, "r") or exit("Unable to open file!");
        //Output a line of the file until the end is reached
        while(!feof($file))
        {
            echo fgets($file). "<br />";
        }
        fclose($file);
    }

function writemyfile($thefilename,$data,$mode) 
    {
        $myfile=fopen($thefilename,$mode);
        fwrite($myfile, $data); // added + "\n" here and responses turned 0
        fclose($myfile);
    }  
?>
<html>
    <head>
        <title> Zain's Test Site</title></head>
    <body>
        <form method="post" action="<?php echo $php_self ?>">
            <input type="text" name="sometext" value="<?php echo $thetext ?>" >
            <input type="submit" name="Submit" value="Click this button">
        </form>
        <?php readmyfile("testFile.txt"); ?>
    </body>

4

5 に答える 5

1
$thetext."\n"

PHP では "." を使用して文字列を連結しますが、JavaScript では "+" を使用します。

于 2011-05-26T15:18:07.553 に答える
1

次のように $thetext 変数に改行文字 (\n) を追加してみてください:

$thetext=$_POST['sometext'] . "\n";

「.」を忘れずに使用してください。連結演算子として使用し、改行文字を二重引用符で囲みます。

于 2011-05-26T15:18:11.940 に答える
1

HTML用のbrの代わりに改行「\ n」を使用してください

于 2011-05-26T15:18:36.423 に答える
1

$text = $text."\n"?

エラー、ここに回答を記入するためのテキストがいくつかあります

于 2011-05-26T15:18:52.093 に答える
1
 fwrite($myfile, $data); // added + "\n" here and responses turned 0

連結文字列演算子は (.) ではなく (+) です

このようにスクリプトを単純化することもできます

 echo nl2br(get_file_contents($file));
于 2011-05-26T15:19:41.993 に答える