PHP Help
Can anyone tell me wut is wrong with this
<?php
PHP
$file = 'data/$pagename.txt';
$fp = fopen($file, 'w+');
$contents = fread ($fp, ($file));
<form action=\" PHP_SELF \">
<textarea name=\"pagedit\" cols=\"45\" rows=\"10\">
$contents
</textarea><br>
<input type=\"submit\" name=\"submit\" value=\"Save Changes\">
</form>
PHP
$post = '$pagedit';
fwrite($fp, $post);
fclose($fp);
?>
necrotic posted this at 04:18 — 14th March 2003.
He has: 296 posts
Joined: May 2002
<?php
<form action=\" PHP_SELF \">
?>
should be
<?php
<form action=\" echo $PHP_SELF; \">
?>
Short-hand PHP (<?...?>) is not recommened.
[James Logsdon]
zollet posted this at 06:49 — 14th March 2003.
He has: 1,016 posts
Joined: May 2002
Hmm... that piece of code is no good. There are too many errors and it doesn't even make sense. What exactly are you trying to do theyapps?
Renegade posted this at 10:05 — 14th March 2003.
He has: 3,022 posts
Joined: Oct 2002
Or if your lazy...
<?php
<form action=\"=$PHP_SELF;\">
?>
zollet posted this at 14:35 — 14th March 2003.
He has: 1,016 posts
Joined: May 2002
You don't need the ; if you're using <?= $var ?>
Suzanne posted this at 16:24 — 14th March 2003.
She has: 5,507 posts
Joined: Feb 2000
really? didn't know that, nifty!
zollet posted this at 18:24 — 14th March 2003.
He has: 1,016 posts
Joined: May 2002
<?= basically means echo/print. Here are some other examples how it could be used..
<?= "Hello World!" ?>
<?= "Your name is: " . $name ?>
<?= 3 * 5 ?>
<?= phpinfo() ?>
etc...
Suzanne posted this at 18:34 — 14th March 2003.
She has: 5,507 posts
Joined: Feb 2000
hot damn!
zollet posted this at 19:49 — 14th March 2003.
He has: 1,016 posts
Joined: May 2002
Yeah
Renegade posted this at 01:35 — 15th March 2003.
He has: 3,022 posts
Joined: Oct 2002
Cool, never knew that
nike_guy_man posted this at 03:06 — 15th March 2003.
They have: 840 posts
Joined: Sep 2000
You learn something new every day, dontcha?
I'm not sure if I understand the concept of this script, but here's my shot at a rewrite
<?php
PHP
if (isset($_POST[pagedit])) {
$post = '$_POST[pagedit]';
$file = 'data/$pagename.txt';
$fp = fopen($file, 'w+');
fwrite($fp, $post);
fclose($fp);
}
$file = 'data/$pagename.txt';
$fp = fopen($file, 'r');
$contents = fread ($fp, ($file));
<form action=\"=$PHP_SELF \" method=\"POST\">
<textarea name=\"pagedit\" cols=\"45\" rows=\"10\">
=$contents
</textarea><br>
<input type=\"submit\" name=\"submit\" value=\"Save Changes\">
</form>
?>
First visit puts the form up with the current contents
Once submitted, it updates the file and reprints the form
However, you have $file = 'data/$pagename.txt' Where does $pagename come from? That's also not clean code
You had fopen ($file, 'w+') which is for writing, you needed fopen ($file, 'r')
Make sense?
I'm sure I made some stupid mistake in my fix there, but I cleaned it up a lot!
Mark Hensler posted this at 07:57 — 15th March 2003.
He has: 4,048 posts
Joined: Aug 2000
<?php
PHP
$file = \"data/$pagename.txt\";
if (isset($_POST[pagedit])) {
$post = $_POST[pagedit];
$fp = fopen($file, 'w+');
if ($fp) {
fwrite($fp, $post);
fclose($fp);
}
}
<form action=\"= $PHP_SELF \" method=\"POST\">
<textarea name=\"pagedit\" cols=\"45\" rows=\"10\">
readfile($file);
</textarea><br>
<input type=\"submit\" name=\"submit\" value=\"Save Changes\">
</form>
?>
$file = "data/$pagename.txt";
Must use double quotes, or else $pagename will not be expanded.
Same thing for $post = $_POST[pagedit];
PHP Docs:
readfile()
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.