Page 2 of 2

Posted: Sun Mar 30, 2003 4:22 pm
by volka
hmmm...maybe it's easier if you explain what the script is meant to do and why (step by step).
I do not like it either but adding usefull comments that explain the script is very helpful ;)

Posted: Sun Mar 30, 2003 4:36 pm
by AbrasiveRock
Well, I'm just trying to make a page that you can type text into, press a button, it goes to a second page, pastes the text to that page (I got everything to work up to this point), & the tricky part is I want that text to the left to stay even as new text is posted in the text box at the bottom of this new page and sent to the top of the area on the right. I hope that all made sense.

Posted: Sun Mar 30, 2003 4:40 pm
by volka
like a comment system? Users are able to add a new text block?

Posted: Sun Mar 30, 2003 4:44 pm
by AbrasiveRock
yep

Posted: Sun Mar 30, 2003 5:12 pm
by volka
does

Code: Select all

<html>
	<head>
		<title>second page</title>
	</head>
	<body style="color: blue;">
		<table border="1" BGCOLOR="#303030" width="100%" height="100%">
			<TR>
				<td width="25%" valign="top">
					<pre>
<?php
	if (is_file('data.txt'))
		$content = file('data.txt');
	else
		$content = array();
	
	foreach($content as $line)
		echo htmlentities($line);
?>
					</pre>
				</td>
			</tr>
			<tr>
				<td>
<?php
	if (!isset($_POST['userName']) || empty($_POST['userName']))
		echo 'you have to provide a username before posting';
	else if(!isset($_POST['NothingRightNow']) || empty($_POST['NothingRightNow']))
	{
		echo $_POST['userName'], ' adds:';
?>
					<form action="secondpage.php" method="POST">
						<input type="hidden" name="userName" value="<?php echo $_POST['userName']; ?>" />
						<textarea name="NothingRightNow" COLS="63" ROWS="1"></textarea>
						<input type="submit" name="submit" value="GO!" />
					</form>
<?php		
	}
	else
	{
		$fp = fopen('data.txt', 'a');
		fwrite($fp, $_POST['userName']." wrote:\n\r");
		fwrite($fp, $_POST['NothingRightNow']);
		fwrite($fp, "\n\r");
		fclose($fp);
		echo '<fieldset><legend>added</legend>', htmlentities($_POST['NothingRightNow']), '</fieldset>';		
	}
?>
				</td>
			</tr>
		</table>
	</body>
</html>
fit your needs somehow?
(it's the best I can do right now, it's 1am here and I should be in bed for couples of hours ;) )

Posted: Sun Mar 30, 2003 5:30 pm
by AbrasiveRock
Ok, I think this may work. There are some tweaks that you made that I need to mess with, but this is at least a step forward.


I am more interested in the learning part of it all than the output. So I have to ask, was my version messed up that bad? Yikes! I didn't realize I was that far off. :oops:

Posted: Mon Mar 31, 2003 2:56 am
by volka
I changed the table structure. It was only meant as (hopefully working) example ;)
My guess was that you missed the point that secondscript.php will at least be called twice and between each request no variables are stored. What has been posted the first time is gone the second time. Http is stateless and php cannot distinguish between different users/requests (sessions can but only because the client is sending an almost unique id with each! request).
I changed the script a bit and added some comments

Code: Select all

<html>
	<head>
		<title>second page</title>
	</head>
	<body style="color: blue;">
		<table border="1" BGCOLOR="#303030" width="100%" height="100%">
			<tr>
				<td width="25%" valign="top">
<?php
/**
	the first table cell always contains
	all previous messages stored in data.txt
	it's already formatted so just send the content to the client
*/
if (is_file('data.txt'))
	readfile('data.txt');
?>
				</td>
			</tr>
			<tr>
				<td>
<?php
/*
	the first page should send a parameter userName
	but users might have called secondpage.php directly
	or have typed nothing into the textbox
*/
if (!isset($_POST['userName']) || empty($_POST['userName']))
	echo 'you have to provide a username before posting';
else if(!isset($_POST['NothingRightNow']) || empty($_POST['NothingRightNow']))
{
/**
	a username has been sent but still no text to add
	output a form to enter the text
	and add the username; this script will check again for it the next time it is called
*/	
	echo $_POST['userName'], ' adds:';
?>
				<form action="secondpage.php" method="POST">
					<input type="hidden" name="userName" value="<?php echo $_POST['userName']; ?>" />
					<textarea name="NothingRightNow" COLS="63" ROWS="1"></textarea>
					<input type="submit" name="submit" value="GO!" />
				</form>
<?php      
}
else
{
/**
	username and new message have been sent
	now save it
*/

	/**
		first output something to prevent hasty users from going back and submit the message again
		ok, technically this prevents nothing, but ....
	*/
	echo '<p>adding</p>';
	flush();
	/**
		preparing the new message. The 'output-only' part of the script does not format the text
		so build something usefull here; I like fieldsets 
		There are two reasons for building it in memory (as one string)
			- display exactly what is written to the file
			- there's no code to handle concurrent requests; at least keep the file open as short as possible
	*/
	$newContent = '<fieldset><legend>' . htmlentities(trim($_POST['userName'])) . " wrote<legend>\r\n"
		. htmlentities($_POST['NothingRightNow'])
		. "</fieldset>\r\n";
	echo $newContent;
	flush();
	
	$fp = fopen('data.txt', 'a');
	fwrite($fp, $newContent);
	fclose($fp);
	echo 'done.';
}
?>
				</td>
			</tr>
		</table>
	</body>
</html>

Posted: Tue Apr 01, 2003 3:18 pm
by AbrasiveRock
Hmm? So is there one line that did the writing to the 'data.txt' or was it a combination of several?

Posted: Tue Apr 01, 2003 7:00 pm
by volka

Code: Select all

// preparing the formatted text-line
$newContent = '<fieldset><legend>' . htmlentities(trim($_POST['userName'])) . " wrote<legend>\r\n"
      . htmlentities($_POST['NothingRightNow'])
      . "</fieldset>\r\n";
// display the new content-line
   echo $newContent;
   flush();

// open data.txt in 'append' mode, write-functions will add at the end of file
   $fp = fopen('data.txt', 'a');
// write the prepared content-line to data.txt
   fwrite($fp, $newContent);
// php would do so at the end of the script but let's close data.txt here and now
   fclose($fp);
// let the user now...
   echo 'done.';
;)