I have just started learning PHP, and I am trying to create a simple script allowing people to add a short message to a webpage (kind of a mini-guestbook..) I use a text file to store the messages.
It works fine on localhost, but after uploading to my web directory, a weird error occurs. The message I typed in the text area didn't appear when I pressed submit. I looked in the text file, and noticed that it did appear there. I wrote another message and pressed submit; the message I wrote last time appears on the page, but not the most recent... and so it continues..
It seems like there is some kind of delay, but I don't understand why this happens, especially since there's no problem when testing on localhost.. 😕
I am using a form that does a POST to the page that currently is showing.
<!-- this is the form -->
<form action="messages.php" method="post">
<textarea name="message" cols="30" rows="5" wrap="physical"></textarea>
<input name="submit" type="submit" value="Send">
</form>
<!-- php code -->
<?php
$message = $_POST['message'];
$file = "messages.txt";
if(!empty($message)) {
$descriptor = fopen ($file, "a");
fwrite($descriptor,$message);
}
$link = fopen($file, "r");
$contents = fread($link, filesize($file));
fclose($link);
echo $contents;
?>