Hi frends,
i m new to php...
so please forgive me if i asked any foolish question.
i wanted to know...abt comment script.
here this is my file.
1.php:
<html><head>
<title>Praaactice</title>
</head><body><h1> Welcome </h1>
<hr>
<?php
// giving variable names to form data
$name = $_POST['name'];
$email = $_POST['email'];
$blog = $_POST['blog'];
$comments = $_POST['comments'];
//displaying & printing form data on page
echo "Posted by: ";
echo "<a href=" . $blog . ">" . $name . "</a>";
echo "<BR>";
echo $comments;
?>
<?php
//writing form data to text file, 'data1.txt'
$handle= fopen("data1.txt","a");
fwrite($handle,'name' . "\n");
fwrite($handle, '~');
fwrite($handle,'email' . "\n");
fwrite($handle, '~');
fwrite($handle,'blog' . "\n");
fwrite($handle, '~');
fwrite($handle,"comments" . "\n\n");
fwrite($handle, '|~|');
fclose($handle);
?>
<hr>
<!-- html form below -->
<form action="1.php" method="post">
Name:<br> <input type="text" name="name" /><br>
Email:<br> <input type="text" name="email" id="email"/><br>
Website/Blog url:<br><input type='text' name='blog' id='blog' value='http://' /><br>
Comments:<br><textarea rows=5 cols=27 name='comments' id='comments'>
</textarea><br>
<input type="submit" value='Submit'/>
</form>
<!-- end of form -->
</body>
</html>
the problem i face is,
when i type 1st comment, it shows on the page.
& then, when i type, 2nd comment,
the 2nd comment replaces the 1st comment,
the 1st comment disappears from the screen.
the 3rd comment makes the 2nd comment disappear.
so on.
how can i get all the comments on the page?
comment script
Moderator: General Moderators
Re: comment script
You don't appear to be reading the data from the file, the display appears to be for the posted data only.
Here is an example of how to read the existing data from the file:
This should be added above the code:
The code above works on the file data1.txt that has the following sample content:
If you have the data that you are using is not write post a real example. Note what I have done is reading the entire content into a single array that will not work well with very large files, however if you finalise the content I can provide you with an example for reading the file line by line.
Personally I would consider using a field format such as "name=blah blah~\n" to simplify the reading, but obviously this is only a suggestion.

Here is an example of how to read the existing data from the file:
Code: Select all
//reading data from text file, 'data1.txt'
$data = file_get_contents("data1.txt");
// split the results per record
$records = explode("|~|", $data);
foreach ($records AS $record)
{
$record = ltrim($record);
preg_match("/^name\s(.+)\semail\s(.+)\scomments\s(.+)$/", $record, $matches);
//displaying & printing form data on page
echo "Posted by: <a href=\"{$matches[2]}\">{$matches[1]}\"</a><br />{$matches[3]}i<br /><br />";
}
Code: Select all
//displaying & printing form data on page
echo "Posted by: ";
echo "<a href=" . $blog . ">" . $name . "</a>";
echo "<BR>";
echo $comments;
Code: Select all
name
First Person
email
first@person.com
comments
abc xyz 123 890
|~|
name
New Person
email
new@person.test
comments
I am testing what we are doing.
|~|
Personally I would consider using a field format such as "name=blah blah~\n" to simplify the reading, but obviously this is only a suggestion.