This should tell you how to do it: PHP Tutorial - File Write
I'm just fiddling with a bit php that adds some data from a form to a mysql database. At the same time this form is submitted I want to add the same data to a text file.
Can anybody help me with how to achieve this?
The file goes back to itself when submitted so the user can submit something different straight away. As confirmation of submitting the previous one successfully I was wanting to put something like:
You have successfully added $string
How would I go about doing this?
Can you post some code to show how far you have got, then we can help you.
I have a form on a page with a field called 'txtTitle'. When this form is submitted it adds that info to a database and then goes back to the same page so they can add something again. All I want is to show what they have just added so they know they have been successful.
Something like print $txtTitle
I don't want to post code because I'm a little ashamed of it. Any help is appreciated
I wouldn't be ashamed of your code - you should see the horror that is some of mine! I think there a generally two ways of doing what you want, if I understand it correctly.
The first is to use a cookie or session to pass a couple of variables - a flag and the submitted text. You could then reload the page, check the flag and
if flag = 1 then
flag = 0
I'd probably use the first method myself along with a test to make sure they were successful.
I would store the input data in a variable so like:
I would then have:PHP Code:
$datasent = $_POST["txtTitle"];
This should solve your problem.PHP Code:
I would store the text entered in a session variable, adding each new text entry to it. This can then be printed when the page reloads giving an audit trail of all text entered in that session.
Thanks for all the help so far - I'm getting there. I have just one thing left to do, and need help again.
I have managed to open, append and close a text file in the same location of the php files, but now need to open, append and close a text file that is stored on a different server.
Can anybody help me?
The code I have so far for this bit is:
The whitelist.txt bit needs to be pointing to \\SERVER\SHARE\whitelist.txtCode:$myFile = "whitelist.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = $_POST['txtAddress']; fwrite($fh, $stringData); fwrite($fh, "\r\n"); fclose($fh);
map the share to the server?
I've tried doing that - I mapped it to G: and put in place G:\whitelist.txt but it said it had failed to open the file.
hmm a quick google says writing to windows network drives from php is massively problematic. you could maybe call up a local batch file that do the copying for you.
or stick ftp on server so you can php > ftp instead.
There's got to be a better way surely?
There are currently 1 users browsing this thread. (0 members and 1 guests)