HTML/PHP form questions
Posted: Mon Apr 23, 2012 6:29 pm
Hello all. I know writing to a text file is not generally the way to go, but it is needed for an assignment and I have gotten about as far as I can get. The HTML and the posting to a text file works. Also getting the data works for bringing in all the data from a text file. Currently I am getting a table row for even the blank lines that go between records. What I really need to viewComments submit button results to show is all my data in a table but there should not be a table row for any blank rows. I have tried working with fgets and fread but am getting lost.
Here is my PHP
Code: Select all
<html>
<head>
<title>Week 5 Guestbook</title>
</head>
<body>
<h1 align="center">Please Sign Our Guestbook</h1>
<form action="thankyou.php" method="POST" name="form">
<table>
<tr>
<th>Name:</th>
<td><input type="text" maxlength="32" size="20"
name="user" id="user"></td>
</tr>
<tr>
<th>Comments:</th>
<td><textarea name="comment" cols="25" rows="7"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submitData" value="Submit Details"></td>
<td><input type="submit" name="viewComments" value="View All Comments"></td>
</tr>
</table>
</form>
</body>
</html>
Code: Select all
<?php
if(isset($_POST['submitData'] )) {
$fp = fopen("guestbook.txt", "a+");
fputs($fp, "$_POST[user]\r\n");
fputs($fp, "$_POST[comment]\r\n\r\n");
fclose($fp);
echo "<font size=18>Thank You For Visiting</font>";
}
else if (isset ($_POST['viewComments'] ))
{
function makeContent() {
$fp = fopen("guestbook.txt", "r");
if($fp) {
while(!feof($fp)) {
$row = fgets($fp, filesize("guestbook.txt"));
$bicks = preg_split("/::/", $row);
echo "<table border='1' width='100'>";
echo"<tr>";
echo "<td size=10>$row</td>";
echo "</tr>";
} fclose($fp);
}
}
echo makecontent();
}
?>