开发者

PHP, Line break when writting to a file

I am trying to keep a running total of all the responses to a form I have written, but I am having trouble making it so that each response takes a new line. I have my code down below. I just want it so that it is easier to read because right now what happens is that all the responses are jammed together would like to have each one on a new line. I tried a few things and have them commented in the code and what the result was. Thanks in Advance.

<?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("t开发者_C百科estFile.txt"); ?>
    </body>


Can you try appending the newline character (\n) to the $thetext variable like this:

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

Remember to use '.' as the concatenation operator, and use double-quotes around the newline character.


$thetext."\n"

in php you concatenate strings using ".", you use "+" in javascript.


Use newline "\n" instead of the br's which is for html


$text = $text."\n" ?

Err here's some more text to fill out the answer


 fwrite($myfile, $data); // added + "\n" here and responses turned 0

the concat string operator is (.) not (+)

you can also simplify your script thusly

 echo nl2br(get_file_contents($file));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜