dennisvdz Posted May 18, 2009 Share Posted May 18, 2009 I'm quite new in PHP, so I want a example only. Please don't post any parts, becouse I can learn only if I see a code and self find what ever line does . Can someone make me a little example script, that will add every time, someone presses on a button, the text, that is containing in the textbox, get added above a .txt file? Like [TEXTBOX] [bUTTON] [TXT FILE: END TXT FILE] If I will type 'Hi' in the textbox, and then press on the button, it will be like this: [TEXTBOX] [bUTTON] [TXT FILE: Hi END TXT FILE] If I will type now 'Rofl' in the box, and press again, it will be like this: [TEXTBOX] [bUTTON] [TXT FILE: Rofl Hi END TXT FILE] But if I typed notting in the textbox, notting happens too when I press the button. If it's possible, it would be fine if, when the button has been pressed, you will get redirected to the page 'succes.php' Thank you, Dennis. Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/ Share on other sites More sharing options...
Maq Posted May 18, 2009 Share Posted May 18, 2009 Can someone make me a little example script, that will add every time, someone presses on a button, the text, that is containing in the textbox, get added above a .txt file? There are plenty of examples online, why don't you give it a try, fwrite & submit. Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-836603 Share on other sites More sharing options...
dennisvdz Posted May 18, 2009 Author Share Posted May 18, 2009 Aha. I understand fwrite() now a bit. I actually don't know anything about connecting them. I found this about sumbit. How can I activate a pice of PHP code, that takes the value into the box 'text' with it? <html> <body> <form action="Something.php" method="post"> Text: <input type="text" name="text" /> <input type="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-836633 Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 In Something.php, use $_POST['text']. You can also use $_REQUEST['text'] or filter_input(INPUT_POST, 'text') Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-836639 Share on other sites More sharing options...
BobcatM Posted May 18, 2009 Share Posted May 18, 2009 <?php $filename = "test01.txt"; //the name of our file. $content = "This is our test file"; //what we will be writing to our file. $strlength = strlen($content); //gets the length of our $content string. $create = fopen($filename, "w"); //uses fopen to create our file. $write = fwrite($create, $content, $strlength); //writes our string to our file. $close = fclose($create); //closes our file echo("File Created, Click <a href='$filename'> Here </a> to view it."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-836641 Share on other sites More sharing options...
Maq Posted May 18, 2009 Share Posted May 18, 2009 This will post to the itself, (the same page you're on): if(isset($_POST['submit'])) { echo "POST text: " . $_POST['text']; //execute code } ?> </pre> <form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="POST"> Text: </form> <br><b NOTE - Please use tags around code. Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-836643 Share on other sites More sharing options...
dennismonsewicz Posted May 18, 2009 Share Posted May 18, 2009 here is a good tutorial on appending things to a txt file http://www.tizag.com/phpT/fileappend.php Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-836644 Share on other sites More sharing options...
dennisvdz Posted May 19, 2009 Author Share Posted May 19, 2009 Ok. I tried this stuff: SITE - store.txt - add.php - index.php ERROR: Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/www/testserver/add.php on line 3 FILES: store.txt: Empty add.php: <?php $filename = 'store.txt'; $somecontent = "Name: $_POST["name"] \n"; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> index.php: <html> <body> <form action="add.php" method="post"> Name: <input type="text" name="name" /> <input type="submit" /> </form> </body> </html> Does someone knows whats wrong? . Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-837227 Share on other sites More sharing options...
Maq Posted May 19, 2009 Share Posted May 19, 2009 Change this line to (need single quotes in the POST array): $somecontent = "Name: $_POST['name'] \n"; NOTE - Please use tags around code. Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-837230 Share on other sites More sharing options...
dennisvdz Posted May 19, 2009 Author Share Posted May 19, 2009 I did just found it out (myself, wow). It aren't the quotes, but it must be closed and with a dot. whatever. $somecontent = "Name: " . $_POST["name"] . "\n"; Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-837233 Share on other sites More sharing options...
Maq Posted May 19, 2009 Share Posted May 19, 2009 You can do it either way. Quote Link to comment https://forums.phpfreaks.com/topic/158628-solved-add-item-to-a-txt-file-newby-here/#findComment-837235 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.