Page 1 of 1

HTML/PHP form questions

Posted: Mon Apr 23, 2012 6:29 pm
by cartotech81
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.

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>
Here is my PHP

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();
		
	}
	?>

Re: HTML/PHP form questions

Posted: Mon Apr 23, 2012 11:36 pm
by Robert07
If you want to filter the blank rows all you need to do is add a check for alphanumeric characters in the $row param, like this:

Code: Select all

if (preg_match('/[a-zA-Z0-9]/',$row)) {
  #Echo a table row here, but not the starting table tag (do that above the loop)
}