MrDoug Posted May 28, 2008 Share Posted May 28, 2008 I just began learning php a few days ago and here's what I'm attempting to accomplish... html file takes input from a form sends it to a php file php file writes it to a text file It just wont work though, heres my code can anyone help me please... HTML FILE - (text.html) ------------------------------------------------------------------------ <html> <body bgcolor="#000000"> <center> <form action="text.php" method="post"> <font size="5" color="#FFFFFF">Enter Text</font><br> <br> <font color="#FFFFFF">Input1:</font><br><input type="text" name="input1" size="60"><br> <br> <font color="#FFFFFF">Input2:</font><br><input type="text" name="input2" size="60"><br> <br> <font color="#FFFFFF">Input3:</font><br><input type="text" name="input3" size="60"><br> <br> <font color="#FFFFFF">Input4:</font><br><textarea name="input4" cols="60" rows="4"></textarea><br> <br> <input type="submit" value="Save" name="submit"> </form> </center> </body> </html> PHP FILE - (text.php) --------------------------------------------------------------------- <?php $fp = fopen(info.txt, "a"); fwrite($fp, '$_POST['input1']'); fwrite($fp, '$_POST['input2']'); fwrite($fp, '$_POST['input3']'); fwrite($fp, '$_POST['input4']'); ?> Thanks for your help, MrDoug Quote Link to comment Share on other sites More sharing options...
revraz Posted May 28, 2008 Share Posted May 28, 2008 Does it create the file, does it not create the file, does it create it but it's empty... what doesn't work? Quote Link to comment Share on other sites More sharing options...
prcollin Posted May 28, 2008 Share Posted May 28, 2008 Does it create the file, does it not create the file, does it create it but it's empty... what doesn't work? question on this topic in this code <?php $fp = fopen(info.txt, "a"); fwrite($fp, '$_POST['input1']'); fwrite($fp, '$_POST['input2']'); fwrite($fp, '$_POST['input3']'); fwrite($fp, '$_POST['input4]'); ?> can you subsitute info.txt for $user_name.txt so that it pulls a user name value and creates a text file based off of that? Quote Link to comment Share on other sites More sharing options...
MrDoug Posted May 28, 2008 Author Share Posted May 28, 2008 @revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty @prcollin I dont have any username variables but i could create them, how would that help though Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 28, 2008 Share Posted May 28, 2008 Certainly, although the "." is a reserved character in php used for concatenation, so you have to build the file name like so: $fp = fopen($user_name . '.txt', 'a'); Quote Link to comment Share on other sites More sharing options...
MrDoug Posted May 28, 2008 Author Share Posted May 28, 2008 Thanks, I added that in but it hasn't changed anything anymore ideas Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 28, 2008 Share Posted May 28, 2008 @revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty @prcollin I dont have any username variables but i could create them, how would that help though My guess is that this is a permissions problem. Is this on a windows or Unix server? Quote Link to comment Share on other sites More sharing options...
jonsjava Posted May 28, 2008 Share Posted May 28, 2008 <?php $fp = fopen("info.txt", "a+"); fwrite($fp, '$_POST['input1']'); fwrite($fp, '$_POST['input2']'); fwrite($fp, '$_POST['input3']'); fwrite($fp, '$_POST['input4']'); fclose($fp); ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 28, 2008 Share Posted May 28, 2008 $fp = fopen("info.txt", "a+"); fwrite($fp, '$_POST['input1']'); fwrite($fp, '$_POST['input2']'); fwrite($fp, '$_POST['input3']'); fwrite($fp, '$_POST['input4']'); fclose($fp); ?> His code makes no attempt to read from the filehandle, so there's no reason to open the file with 'a+' mode. Opening with 'a' is fine here and does work. As I posted previously I'm fairly confident this is a file permissions problem. By default this will try to write in the same directory as the executing script, and probably the Apache user doesn't have permissions to write to that directory. Quote Link to comment Share on other sites More sharing options...
prcollin Posted May 28, 2008 Share Posted May 28, 2008 @revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty @prcollin I dont have any username variables but i could create them, how would that help though No i wasnt suggesting an answer just another question on how I could use a variable to save the file. One more thing how do you get it to post to a specific directory again I used to know now i forget i have a directory called philsshit on my server and i want to take this new file and put it in that directory Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 28, 2008 Share Posted May 28, 2008 @revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty @prcollin I dont have any username variables but i could create them, how would that help though No i wasnt suggesting an answer just another question on how I could use a variable to save the file. In answering you I completely blanked out on the fact you wanted to use a variable -- I amended the code example so it uses a variable. As I stated, No problem, you can do it. Quote Link to comment Share on other sites More sharing options...
prcollin Posted May 28, 2008 Share Posted May 28, 2008 Certainly, although the "." is a reserved character in php used for concatenation, so you have to build the file name like so: $fp = fopen($user_name . '.txt', 'a'); GREAT! thanks giz Quote Link to comment Share on other sites More sharing options...
revraz Posted May 28, 2008 Share Posted May 28, 2008 He wasn't the OP. In answering you I completely blanked out on the fact you wanted to use a variable -- I amended the code example so it uses a variable. As I stated, No problem, you can do it. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 28, 2008 Share Posted May 28, 2008 He wasn't the OP. In answering you I completely blanked out on the fact you wanted to use a variable -- I amended the code example so it uses a variable. As I stated, No problem, you can do it. Agreed. I did try and answer the original poster, but he seems to have gone dark -- even PM'd him. I'm about 99.9% sure it's a permissiosn problem. Quote Link to comment Share on other sites More sharing options...
MrDoug Posted May 29, 2008 Author Share Posted May 29, 2008 sry about not responding, I was posting during a free period at school but I had to return to classes. I got your PM, but it said I'm not allowed to send PM's, dunno why. I'm not actually sure it was a permissions problem. I fiddled with it a little more and modified it based on reading other code and somehow it worked. Heres my code, btw I added some things once I got it working and am also now using an html file rather than txt <?php $fp = fopen('info' . '.html', 'a'); fwrite($fp, '<br>'); fwrite($fp, '<br>'); fwrite($fp, '<b>input1:</b>'); fwrite($fp, '<br>'); fwrite($fp, $_POST['input1']); fwrite($fp, '<br>'); fwrite($fp, '<br>'); fwrite($fp, '<b>input2:</b>'); fwrite($fp, '<br>'); fwrite($fp, $_POST['input2']); fwrite($fp, '<br>'); fwrite($fp, '<br>'); fwrite($fp, '<b>input3:</b>'); fwrite($fp, '<br>'); fwrite($fp, $_POST['input3']); fwrite($fp, '<br>'); fwrite($fp, '<br>'); fwrite($fp, '<b>input4:</b>'); fwrite($fp, '<br>'); fwrite($fp, $_POST['input4']); fwrite($fp, '<br>'); fwrite($fp, '<br>'); fwrite($fp, '<hr height="5" width="100%">'); ?> Thanks for all your help, MrDoug Oh, one more thing, how do I make it load a new html page once its written everything, so for instance... user gives input input is saved new page loads saying "thanks for contributing" or whatever Quote Link to comment Share on other sites More sharing options...
prcollin Posted May 29, 2008 Share Posted May 29, 2008 how do you specify the directory it drops into Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 29, 2008 Share Posted May 29, 2008 Oh, one more thing, how do I make it load a new html page once its written everything, so for instance... user gives input input is saved new page loads saying "thanks for contributing" or whatever This is a big reason why people use PHP -- because anything you print or echo is returned to the client as html. So all you need to do is echo out what you want. echo 'Thanks for your submission. '; Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 29, 2008 Share Posted May 29, 2008 how do you specify the directory it drops into Add the path to the fopen() function when you specify the file, rather than just the filename. fopen('/some/path/yourfile.txt', ....) Quote Link to comment Share on other sites More sharing options...
prcollin Posted May 29, 2008 Share Posted May 29, 2008 how do you specify the directory it drops into Add the path to the fopen() function when you specify the file, rather than just the filename. fopen('/some/path/yourfile.txt', ....) so could i do $fp = fopen(userpages/$user_name . '.txt', 'a'); so that it takes the user name input from the form to create the file? Quote Link to comment 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.