|
|
|
|
![]() ![]() |
May 25 2007, 06:04 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 413 Joined: 4-October 06 From: Psychedelic Realms Member No.: 31,079 |
I made a simple flat file text editor, that can show you probably how simple it is to use forms with php and write that data to file.
This example has 2 files, submit.php, and postit.html. Submit.php is used to write title, and some text, and add html tags, and paragraph tags where new paragraphs are. Here's the file with comments. I think that HTML really doesn't need some more explaining. CODE Title: <br /> <input type="text" name="title" size="53"> <br /> Text: <br /> <textarea name="text" rows="20" cols="40"></textarea><br /> <br /> <input type="submit" value="Save"> </form> <?php if (@$_POST['title']=="" || @$_POST['text']==""){ echo "Enter text to save!"; } else { //this is path to file $filename = './postit.html'; //i used $title to strip HTML/PHP tags < >, and added <h2> HTML tag to decorate title $title = "<h2>".strip_tags($_POST['title'])."</h2>\n"; //with this $_POST['text'] is stripped from html/php tags like title, but also preg_replace //is used to replace \n(every enter that u hit when writing tekst), //and instead of that added <p> tags which decorate your new paragraph $tekstmanage = preg_replace('/\n/','</p><p>', strip_tags($_POST['text'])); //i should probably write a whole preg_replace function instead, which i will probably later, //but this $tekst variable is used to add finishing and closing tags //to our tekst manage. you can see how preg_replace adds first closing than starting tags //and with this we just simply add starting before closing and closing after starting tags :confused:;) $tekst = "<p>".$tekstmanage."</p>\n"; //after our variables are defined, we combine them in unique variable which we'll write to file $content = $title.$tekst; //This below is basic write to file function that i found on php.net, and rearanged //i'll just explain it briefly // //this checks weather our filename is writeable (on unix systems like here on trap17) //you need to change permissions to the file that allows writing to world 646 if (is_writeable($filename)){ //if file cant be opened to add -> 'a' <- in the fopen construct //just echo sorry if(!$handle= fopen($filename,'a')){ echo "Sorry, you can't write to file1"; } //if file cant be written to, same if (fwrite($handle, $content) === FALSE){ echo "Sorry, you can't write to file2"; exit; } //if both of those terms are fine just echo the confirmation text echo "You wrote to file ".$filename."<br />"; echo "<a href=\"postit.html\">View</a>"; } //if file isn't writeable else { echo "Sorry, you can't write to file3"; } } ?> Well, there it is, hope u have fun with it. Also this needs a bit more work to be done with it, so i won't post dl link. If you have any questions about the script, you know what to do EDIT: Note to moderators, sorry this is not a real text EDITOR (it only writes), so can u please edit the title, to suit the script better.. Thanks This post has been edited by jlhaslip: May 25 2007, 11:57 AM |
|
|
|
May 25 2007, 09:05 AM
Post
#2
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 144 Joined: 22-March 07 Member No.: 40,472 |
Greetings
man thanx for the script it simple and short. but what is the extention of the file when you save it ??? .......... |
|
|
|
May 25 2007, 12:02 PM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 413 Joined: 4-October 06 From: Psychedelic Realms Member No.: 31,079 |
Greetings man thanx for the script it simple and short. but what is the extention of the file when you save it ??? .......... file extension, and path is given with this variable CODE $filename = './postit.html'; ./ means to look in the current directory, and filename's extension is .html. You can change extension to any one you like just my renaming the file that PHP looks for Yes, it's short, but i posted it mostly for that preg_replace thing, although i admit it needs some more work, so that it writes nicer code than it does now. And soon i'll create a bit more powerfull, but again, simple, text editor (script that reads files, and allows you to edit them), but that needs a bit more work, and probably use of file_get_contents function. I'll look into it when i have more time EDIT: Thank you jlhaslip for changing the title, manipulator, sounds nice This post has been edited by matak: May 25 2007, 12:05 PM |
|
|
|
May 25 2007, 12:25 PM
Post
#4
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 144 Joined: 22-March 07 Member No.: 40,472 |
file extension, and path is given with this variable CODE $filename = './postit.html'; ./ means to look in the current directory, and filename's extension is .html. You can change extension to any one you like just my renaming the file that PHP looks for Yes, it's short, but i posted it mostly for that preg_replace thing, although i admit it needs some more work, so that it writes nicer code than it does now. And soon i'll create a bit more powerfull, but again, simple, text editor (script that reads files, and allows you to edit them), but that needs a bit more work, and probably use of file_get_contents function. I'll look into it when i have more time EDIT: Thank you jlhaslip for changing the title, manipulator, sounds nice Greetings Thanx man for the quick repply i'm looking forward to use this script and see it in action thanx, have a nice day |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th September 2008 - 01:05 PM |