3

I'm only starting to learn code with the help of youtube and other sites, and I've run tino a problem. Here's my code:

<form action="" method="post" id="c">
  <label> Name: <br><input type="text" name="name" size="36"></label><br></br>
  <label> Message: <br><textarea cols="35" rows="5" name="mes"></textarea></label><br></br>
  <input type="submit" name="submit" value="Submit" class="texty" >
</form>

<?php
$post = $_POST["post"];
$name = $_POST["name"];
$text = $_POST["mes"];

if ($post) {

    #WRITE DOWN COMMENTS#

    $write = fopen("c.txt", "a+");
    fwrite($write, "<u><b> $name</b></u><br>$text<br></br>");
    fclose($write);

    #DISPLAY COMMENTS#

    $read = fopen("c.txt", "r+t");
    echo "All comments:<br>";

    while (!feof($read)) {   #this line does the error#
        echo fread($read, 1024);
    }
    fclose($read);
}
else{
    #DISPLAY COMMENTS#

    $read = fopen("c.txt", "r+t");
    echo "All comments:<br>";

    while (!feof($read)) {
        echo fread($read, 1024);
    }
    fclose($read);
}
?>

So I have 2 files, the one where you input your comment and post it, and another .txt file where what you have typed is pasted and then echoed back on the page. I think something may be wrong with the permission of the files, because if i put it to "r" only, then it doesn't give me the error, but then it doesn't save what I try to post... Thank you for reading and replying.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Katsu
  • 41
  • 2
  • 5
  • understand what the error is trying to tell you.. – DirtyBit Aug 15 '15 at 21:42
  • @HawasKaPujaari I think that is the reason he is asking this question. – Maximillian Laumeister Aug 15 '15 at 21:49
  • Warning: feof() expects parameter 1 to be resource, boolean given in /volume1/web/comment.php on line 62 Warning: fread() expects parameter 1 to be resource, boolean given in /volume1/web/comment.php on line 63 This are the two errors that I get, but being new to coding, they don't tell me much :/ – Katsu Aug 15 '15 at 21:54

2 Answers2

1

fopen will return a boolean (FALSE) if the file could not be opened. You should check that $read is not false before trying to pass it to feof. And then figure out why you can't read the file.

if ($read) {
    ...

Your file may be unreadable due to permissions, or if the file itself cannot be found. You are referring to c.txt, but is it in the same directory that PHP is in? You can find out what directory PHP is in with getcwd.

echo getcwd()

Also, your form handler will only enter the write branch if $_POST['post'] has a value, but that name is not used in your form (at least not in the shown example).

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • I've set all file permissions to 777 so that can't be that, and idk how to check if it is false. I was only following the tutorial and after the error popped up, made sure the code was identical. you can see what i'm doing here: www.killthemall.co/test.php Also the post will have a value once you type something in the text box i believe. Correct me if i'm wrong tho. – Katsu Aug 15 '15 at 22:16
  • I edited my answer to address the file mode and where the file is located. As for the `post` value, I don't know. There is no form element named `post` in your example, and your page loads forever until I stop it, and I never get to where the browser will show me the source. – Dan Lowe Aug 15 '15 at 22:31
  • Thanks for explaining. The source is already given in the main question. That's all there is :/ ALso yeah i have no idea why the page keeps on doing it forever. maybe stop it refreshing and check the source then. I'm gonna check if $read is true now. – Katsu Aug 15 '15 at 22:40
  • If you are trying to determine that the form has posted, a common idiom is to check for the submit button, e.g. `if ($_POST['submit']) {` – Dan Lowe Aug 15 '15 at 22:41
0

Thanks Dan, The problem was in the submit command. One was submit and the other one post, so it didn't understand what I was trying to do. PS: Sorry for late reply ;_;

Katsu
  • 41
  • 2
  • 5