PHP Help

They have: 21 posts

Joined: Nov 2001

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

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]

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's picture

He has: 3,022 posts

Joined: Oct 2002

Quote: Originally posted by necrotic

<?php
<form action=\" PHP_SELF \">
?>

should be

<?php
<form action=\" echo $PHP_SELF; \">
?>

Or if your lazy...

<?php
<form action=\"=$PHP_SELF;\">
?>

Sticking out tongue

He has: 1,016 posts

Joined: May 2002

You don't need the ; if you're using <?= $var ?>

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

really? didn't know that, nifty!

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's picture

She has: 5,507 posts

Joined: Feb 2000

hot damn!

He has: 1,016 posts

Joined: May 2002

Yeah Laughing out loud

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Cool, never knew that Laughing out loud

nike_guy_man's picture

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!

Laughing out loud

Mark Hensler's picture

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.