i need to add the value of a text box to a txt file on my server which will then be included in a textarea on the page. how will i do this (i know how to do the include bit)? thanks
i need to add the value of a text box to a txt file on my server which will then be included in a textarea on the page. how will i do this (i know how to do the include bit)? thanks
That ought to work.PHP Code:<?php
$fn = "file.txt";
$file = fopen($fn, "a+");
$size = filesize($fn);
if($_POST['addition']) fwrite($file, $_POST['addition']);
$text = fread($file, $size);
fclose($file);
?>
<form action="<?=$PHP_SELF?>" method="post">
<textarea><?=$text?></textarea><br/>
<input type="text" name="addition"/>
<input type="submit"/>
</form>
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Hi Twey,
Nice code. How would it need to be changed so that instead of 'adding' text to the .txt file, it actually 'replaced' the text in the .txt file?
kind regards,
June
Change the "a+" to "w+" in fopen().
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Produces the error message:Originally Posted by Twey
Warning: fread(): Length parameter must be greater than 0
r+ seems to work without an error.
Your link says the following:
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
I would like to get w+ working.
Last edited by June; 02-21-2006 at 12:28 AM.
Remove the line with the fread() call -- as you're removing all the contents of the file, it doesn't make sense to try to read in said contents.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Hi Twey, firstly many thanks for your help.
Here's what I'm trying to do... use the code as a basic 'text file editor'. The text file is then used as an 'include' in a single webpage. Here's the code.
Code:<?php $fn = "textfile.txt"; $file = fopen($fn, "w+"); $size = filesize($fn); if($_POST['update']) fwrite($file, $_POST['update']); //$text = fread($file, $size); fclose($file); ?> <form action="<?=$PHP_SELF?>" method="post"> <textarea Name="update" cols="50" rows="10"><?=$text?></textarea><br/> <input type="submit" value="Update"/> </form>
By putting // in front of
$text = fread($file, $size);
it now doesn't load the text into the textarea from the text file (which it would need to do to edit the text).
It needs to load the text into the textarea, and be able to replace the content in the text file. At the moment it does one or the other.
If we could then put a WYSIWYG editor on the textarea such as
http://www.dynamicdrive.com/dynamici...itor/index.htm
Then it would make a really fantastic basic CMS.
Also on submit perhaps the form could reload the current form page (so showing the updated text in the textarea), or go to another page?
What do you think?
Last edited by June; 02-22-2006 at 02:37 AM.
PHP Code:<?
if($_POST['Submit']){
$open = fopen("textfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>![]()
Last edited by Pixelcode; 02-22-2006 at 09:36 AM.
Originally Posted by Pixelcode
That's great!
1. On the results page showing what has been submitted (not the text file) it needs a couple of links under the text (because re-entering the url is showing the results and not the form). So to the above, how would I add?
2. Needs to include a free WYSIWYG editor on the text area. Anyone able to do that?Code:<p><a href="./livepage.php">click here to view the live updated webpage</a></p> <p><a href="./mainadminpage.php">click here to view the admin menu</a></p>
And that would be it to make a great little CMS.
Thanks,
June
Bookmarks