|
-
Nov 25th, 2009, 08:50 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] write to file?
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
Code:
$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?
-
Nov 25th, 2009, 09:19 AM
#2
Re: write to file?
input.php
Code:
<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
Code:
<?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.
-
Nov 25th, 2009, 09:29 AM
#3
Thread Starter
Fanatic Member
Re: write to file?
it makes the file but doesn't write the text..
-
Nov 25th, 2009, 10:50 AM
#4
Re: write to file?
My bad, change this:
Code:
$stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text-input']);
to this:
Code:
$stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text_input']);
-
Nov 25th, 2009, 10:52 AM
#5
Re: write to file?
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.
-
Nov 25th, 2009, 11:33 AM
#6
Thread Starter
Fanatic Member
Re: write to file?
i can't belive i missed that one...
thank you very much.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|