Read *.txt -> Textform; Edit Textform-> write to *.txt

A

Anonymous

Guest
Hi Guys,

still a newbie in php, I got a relative simple task to solve but I've been now spending hours and wont get this thing to work..
I can read from the txt-file, and I can write to it, but i can't get both things to work at the same time.

I want to program a little User(Admin) Interface for a Website.
The Admin should be able to edit his "news" area by himself.
What I tried is a simple textfield, which reads the "news.txt" file.
like this:
Code:
$fp=fopen("news.txt","r");
while($line=fgets($fp,1024))
{
echo "$line";
}
fclose($fp);

With the textarea-form it looks like this:
Code:
<form name="form1" method="post" action="config.php">
  <textarea name="textarea" cols="80" rows="10" class="baukasten">
<?php
$fp=fopen("news.txt","r");
while($line=fgets($fp,1024))
{
echo "$line";
}
fclose($fp);
</textarea>

This works perfect so far..
now I want to edit the text in the textbox and submit it to itself (config.php), or if this might be unusefull.. to an other php-file which writes the edited text to the txt-file.
should I go on with my codings ?
ok,
here's the final..

Code:
<form name="form1" method="post" action="config.php">
  <textarea name="textarea" cols="80" rows="10" class="baukasten">
<?php
$fp=fopen("news.txt","r");
while($line=fgets($fp,1024))
{
echo "$line";
}
fclose($fp);


if ($submit) {
$fp=fopen("news.txt","w");
fwrite($fp,$form1);
fclose($fp);
echo ("$form1");
}
?>

</textarea>
  <br>
  <input type="submit" name="Submit" value="Save Changes">
</form>

I guess I got messed up with some variables..
My logic:
Always read the news.txt file when config.php is loaded..
when the submit button is/was pressed then (if $submit...) there have been made some changes.. save them to txt file..
what am i doing wrong ?
should the $line, when reading a file, be replaced by an array ?
is "fwrite($fp,$form1);" legal ? does the variable $form1 have the edited text as a value ?

thanks for your help..
Frank[/quote]
 
In your write step, you're writing the variable $fp to the file, but the contents of the textarea aren't in $fp, it's in $_POST['textarea'] (btw, pick a better name for that thing).
 
thx Swirlee.

I noticed another (or same) mistake..
actually I solved it this way (after changing $form1 variable to $textarea !)

looks something like this:
The name of the file itself is "config.php"

Code:
<form name="form1" method="post" action="config.php">
  <textarea name="textarea" cols="80" rows="10" class="baukasten">
<?php
$fp=fopen("news.txt","r+");
while($line=fgets($fp,1024))
{
echo "$line";
}
fclose($fp);

if (isset($_POST["Submit"])) 
 {
 
$fp=fopen("news.txt","w"); 
fwrite($fp,$textarea);
fclose($fp);
}
?>
</textarea>
  <br>
  <input type="submit" name="Submit" value="Änderungen Speichern">
<?php
if (isset($_POST["Submit"])) 
 { echo ("Seite wurde abgespeichert!");
 }
 ?>
</form>

Works almost "awesome", I am kind of very proud of it..
I only got a "refresh" (IE: F5) problem, which I'm to solve later these days (I hope).
 
You'll always get refresh problems with POST forms, it's pretty much unavoidable.
 
Back
Top