Click to See Complete Forum and Search --> : [RESOLVED] write to file?
Justa Lol
Nov 25th, 2009, 07:50 AM
how can i write text into a textbox and then press a button and save the text thats in the textbox to a file?
edit: i found this code:
http://www.tizag.com/phpT/filewrite.php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
how can i make this code save from a textbox instead of a string?
... and make the linebreaks save with a <br/> tag?
I_Love_My_Vans
Nov 25th, 2009, 08:19 AM
input.php
<form action="process.php" method="post">
<input type="hidden" name="submit" />
<textarea name="text_input" rows="2" cols="20"></textarea>
<input type="submit" name="submit" value="Write File" />
</form>
process.php
<?php
if(isset($_POST['submit'])) {
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text-input']);
fwrite($fh, $stringData);
fclose($fh);
} else {
header('Location: input.php');
exit;
}
?>
Right, so we have input.php, slap this code in the <body> section, you can type info into the textarea and when you hit submit it will send that POST data to the process.php script, it will check to see if the form has been submitted (you can add additional conditions if desired), then replaces the \n or \r with <br/>.
This code is not tested / written from memory, so don't expect it to work first time. Be sure to tell me how you get on.
Justa Lol
Nov 25th, 2009, 08:29 AM
it makes the file but doesn't write the text..
I_Love_My_Vans
Nov 25th, 2009, 09:50 AM
My bad, change this:
$stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text-input']);
to this:
$stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text_input']);
kows
Nov 25th, 2009, 09:52 AM
you're referring to $_POST['text-input'] when saving the text with fwrite(); however, your form field is named text_input. simply change that to $_POST['text_input'].
a somewhat better way of checking for a form submission is checking the REQUEST_METHOD ($_SERVER['REQUEST_METHOD']), by the way. for a small application, it may be un-needed, however.
edit: oops, I guess you beat me to it.
Justa Lol
Nov 25th, 2009, 10:33 AM
i can't belive i missed that one...
thank you very much.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.