I do not like it either but adding usefull comments that explain the script is very helpful
From a form to a database?
Moderator: General Moderators
-
AbrasiveRock
- Forum Commoner
- Posts: 40
- Joined: Sat Feb 08, 2003 10:47 pm
- Location: Olympia,WA
- Contact:
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.
-
AbrasiveRock
- Forum Commoner
- Posts: 40
- Joined: Sat Feb 08, 2003 10:47 pm
- Location: Olympia,WA
- Contact:
doesfit 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
)
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>(it's the best I can do right now, it's 1am here and I should be in bed for couples of hours
-
AbrasiveRock
- Forum Commoner
- Posts: 40
- Joined: Sat Feb 08, 2003 10:47 pm
- Location: Olympia,WA
- Contact:
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
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>-
AbrasiveRock
- Forum Commoner
- Posts: 40
- Joined: Sat Feb 08, 2003 10:47 pm
- Location: Olympia,WA
- Contact:
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.';