Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to separate text files

Status
Not open for further replies.

stc7outlaw

Programmer
Mar 7, 2002
3
0
0
US
I am a new programmer and I have gathered some code together to form this one. I want this code to write to 4 separate text files without the other ones changing. IF you try this code you will see what i mean. I have 4 separate update files update1.txt update2.txt update3.txt update4.txt. I want to be able to modify each of them when i want to but I want to keep it all on the same page. This page is called line_updates.php. Here is the code:
<HTML>
<HEAD>
<TITLE></TITLE>
<META name=&quot;description&quot; content=&quot;&quot;>
<META name=&quot;keywords&quot; content=&quot;&quot;>
<META name=&quot;generator&quot; content=&quot;CuteHTML&quot;>
</HEAD>
<BODY BGCOLOR=&quot;#FFFFFF&quot; TEXT=&quot;#000000&quot; LINK=&quot;#0000FF&quot; VLINK=&quot;#800080&quot;>
This is the current Text for Today's FHS News:<br>
<?php

$fp = fopen(&quot;update1.txt&quot;, &quot;w&quot;); // Open a file pointer
fputs($fp, $update1); // Write the value of $test
// to file.txt
echo (&quot;Line 1: $update1<br>&quot;);

$fq = fopen(&quot;update2.txt&quot;, &quot;w&quot;); // Open a file pointer
fputs($fq, $update2); // Write the value of $test
// to file.txt
echo (&quot;Line 2:$update2<br>&quot;);
$fr = fopen(&quot;update3.txt&quot;, &quot;w&quot;); // Open a file pointer
fputs($fr, $update3); // Write the value of $test
// to file.txt
echo (&quot;Line 3:$update3<br>&quot;);
$fs = fopen(&quot;update4.txt&quot;, &quot;w&quot;); // Open a file pointer
fputs($fs, $update4); // Write the value of $test
// to file.txt
echo (&quot;Line 4:$update4<br>&quot;);

fclose($fp);
fclose($fq);
fclose($fr);
fclose($fs);


?>

<form method=post action=<? echo $PHP_SELF; ?> >
<input type=&quot;text&quot; name=&quot;update1&quot;>
<input type=submit name=submit value=&quot;write file&quot;>
</form>



<form method=post action=<? echo $PHP_SELF; ?> >
<input type=&quot;text&quot; name=&quot;update2&quot;>
<input type=submit name=submit value=&quot;write file&quot;>
</form>


<form method=post action=<? echo $PHP_SELF; ?> >
<input type=&quot;text&quot; name=&quot;update3&quot;>
<input type=submit name=submit value=&quot;write file&quot;>
</form>



<form method=post action=<? echo $PHP_SELF; ?> >
<input type=&quot;text&quot; name=&quot;update4&quot;>
<input type=submit name=submit value=&quot;write file&quot;>
</form>


</BODY>
</HTML>

Please modify the code so I can use it and update the files separately to the 4 separate files. THANK YOU.
 
Firstly, you should check to make sure that the form has been posted, otherwise don't edit the files. Then I'd just use hidden variables...

ala;

<?
if ($REQUEST_METHOD == &quot;POST&quot;) {
if ($update1) {

$fp = fopen(&quot;update1.txt&quot;, &quot;w&quot;); // Open a file pointer
fputs($fp, $update1); // Write the value of $test
// to file.txt
echo (&quot;Line 1: $update1<br>&quot;);
}

elseif ($update2) {

....

}
print &quot;Done!&quot;;
}
else { // if the form hasn't been submitted...
?>
<form method=post action=<? echo $PHP_SELF; ?> >
<input type=&quot;hidden&quot; name=&quot;update1&quot; value=&quot;1&quot;>
<input type=&quot;text&quot; name=&quot;update1&quot;>
<input type=submit name=submit value=&quot;write file&quot;>

<form method=post action=<? echo $PHP_SELF; ?> >
<input type=&quot;hidden&quot; name=&quot;update2&quot; value=&quot;1&quot;>
<input type=&quot;text&quot; name=&quot;update2&quot;>
<input type=submit name=submit value=&quot;write file&quot;>

...

<? } ?>
</form>
 
Make sure you put in the

fclose($fp);

after the fputs() line too... I forgot that bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top