Johng123 Posted January 4, 2010 Share Posted January 4, 2010 Okay so I do not really know php that well and I am trying to write a simple website code in it but I am running into a problem. I have a form and I want it to add to the end of a list. I have a text file called codes.txt on the server and idk why this won't work. <html> <body> Your friend code <?php echo $_POST['fcode']; $myFile = "codes.txt"; echo $myFile; $fh = fopen($myfile,'a+') or die("can't open file"); fwrite($fh , $fcode); ?> has been added to the list </body> </html> site here: http://invitemasteronline.t35.com/test.html Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/ Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 i believe this is along the lines of what you want. But I would suggest imposing some security restrictions on who can send the form and what data can be sent, you dont want just anybody posting huge amounts of unregulated or even malicious data to a file on your server. <?php function show_form() { ?> <form action="" method="post"> Friend Code: <input type="text" name="fcode" /> <input type="submit" value="submit" /> </form> <?php } $friend_file = 'friends.txt'; $friend_content = file_get_contents($friend_file); if(isset($_POST['fcode'])) { $friend_code = $_POST[fcode]; $friend_content = $friend_content . "\r\n" . $friend_code; if(file_put_contents($friend_file,$friend_content)) { print $friend_code . ' has been added to the friends file.<hr />'; } } show_form(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987891 Share on other sites More sharing options...
teamatomic Posted January 4, 2010 Share Posted January 4, 2010 Whats $fcode? $fcode=$_POST['fcode'] HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987893 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 Yeah i just caught that too OP: you can just change fwrite($fh , $fcode); to fwrite($fh , $_POST[fcode]); Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987895 Share on other sites More sharing options...
Johng123 Posted January 4, 2010 Author Share Posted January 4, 2010 i believe this is along the lines of what you want. But I would suggest imposing some security restrictions on who can send the form and what data can be sent, you dont want just anybody posting huge amounts of unregulated or even malicious data to a file on your server. <?php function show_form() { ?> <form action="" method="post"> Friend Code: <input type="text" name="fcode" /> <input type="submit" value="submit" /> </form> <?php } $friend_file = 'friends.txt'; $friend_content = file_get_contents($friend_file); if(isset($_POST['fcode'])) { $friend_code = $_POST[fcode]; $friend_content = $friend_content . "\r\n" . $friend_code; if(file_put_contents($friend_file,$friend_content)) { print $friend_code . ' has been added to the friends file.<hr />'; } } show_form(); ?> thanks but I don't really wanna change the whole thing, cause then I don't understand what's happening at all. I just wanna figure out why mine isn't working Whats $fcode? $fcode=$_POST['fcode'] HTH Teamatomic sorry, that was the input from a form. Also its not even reading the file still I think. Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987898 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 Yeah i just caught that too OP: you can just change fwrite($fh , $fcode); to fwrite($fh , $_POST[fcode]); did you try that? Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987901 Share on other sites More sharing options...
Johng123 Posted January 4, 2010 Author Share Posted January 4, 2010 Yeah i just caught that too OP: you can just change fwrite($fh , $fcode); to fwrite($fh , $_POST[fcode]); did you try that? yupp...didn't work...it's not even reading the file like I get an error at the or die() part Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987906 Share on other sites More sharing options...
teamatomic Posted January 4, 2010 Share Posted January 4, 2010 Why dont you give the full path the the file. if its in the same folder as the script then ./file.txt otherwise /path/to/file.txt. And if you have control of the server and the code is not for distribution and you have php v5+ then use file_put_contents("$myFile", "$fcode", FILE_APPEND, LOCK_EX); who owns the file? what are the perms? so what if its form output. To use $fcode you need to give it a value also...show us the error you get..."it's broken" is not really good enough HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987930 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 You should try www.php.net/file_get_contents and www.php.net/file_put_contents as well. It's the same kind of thing just easier to use and remember. Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988025 Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 @crabfinger don't just write some code without thinking about it always make sure that heavy operations (or soon to be heavy) are lazy-loaded $friend_file = 'friends.txt'; $friend_content = file_get_contents($friend_file); if(isset($_POST['fcode'])) { $friend_code = $_POST[fcode]; $friend_content = $friend_content . "\r\n" . $friend_code; if(file_put_contents($friend_file,$friend_content)) { print $friend_code . ' has been added to the friends file.<hr />'; } } In the above code is the file (friends.txt) data read but not used until the form is submitted. Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988082 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 @crabfinger don't just write some code without thinking about it always make sure that heavy operations (or soon to be heavy) are lazy-loaded $friend_file = 'friends.txt'; $friend_content = file_get_contents($friend_file); if(isset($_POST['fcode'])) { $friend_code = $_POST[fcode]; $friend_content = $friend_content . "\r\n" . $friend_code; if(file_put_contents($friend_file,$friend_content)) { print $friend_code . ' has been added to the friends file.<hr />'; } } In the above code is the file (friends.txt) data read but not used until the form is submitted. Sorry when i wrote that i was going to add print '<hr />' . $friend_content; to the end of the file for debugging purposes Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988315 Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 How does that put $friend_content = file_get_contents($friend_file); in the correct place? Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988330 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 How does that put $friend_content = file_get_contents($friend_file); in the correct place? $friend_file = 'friends.txt'; $friend_content = file_get_contents($friend_file); if(isset($_POST['fcode'])) { $friend_code = $_POST[fcode]; $friend_content = $friend_content . "\r\n" . $friend_code; if(file_put_contents($friend_file,$friend_content)) { print $friend_code . ' has been added to the friends file.<hr />'; } } print '<hr />' . $friend_content; means that i only have to load the file once because it is being used wether or not the form is posted. Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988397 Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 Then you are better off using: print '<hr />' . nl2br($friend_content); Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988402 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 That may be but i don't see how that relates to your original complaint Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988449 Share on other sites More sharing options...
Johng123 Posted January 5, 2010 Author Share Posted January 5, 2010 @crabfinger don't just write some code without thinking about it always make sure that heavy operations (or soon to be heavy) are lazy-loaded $friend_file = 'friends.txt'; $friend_content = file_get_contents($friend_file); if(isset($_POST['fcode'])) { $friend_code = $_POST[fcode]; $friend_content = $friend_content . "\r\n" . $friend_code; if(file_put_contents($friend_file,$friend_content)) { print $friend_code . ' has been added to the friends file.<hr />'; } } In the above code is the file (friends.txt) data read but not used until the form is submitted. ok thank you soo much that worked great!! Now I am really confused though I tried to change the code, so it would check if the user has tried to submit a code before. It does not work! <html> <body> <?php $friend_file = 'friends.txt'; $friend_content = file_get_contents($friend_file); if(isset($_POST['fcode'])) { $pos = strpos($friend_content, ($_POST['fcode']); if ($pos === false) { $friend_code = $_POST[fcode]; $friend_content = $friend_content . "\r\n" . $friend_code; if(file_put_contents($friend_file,$friend_content)) { print $friend_code . ' has been added to the friends file.<hr />'; } } if ($pos === !false){ print $friend_code . ' is already on the list!<hr />'; } } ?> </body> </html> error is: "Parse error: syntax error, unexpected ';' in /home/freehost/t35.com/i/n/invitemasteronline/addcode.php on line 7" Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988534 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 Change $pos = strpos($friend_content, ($_POST['fcode']); to: $pos = strpos($friend_content, $_POST['fcode']); There seems to have been an error when the code was written, that is line 7. Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988539 Share on other sites More sharing options...
Johng123 Posted January 5, 2010 Author Share Posted January 5, 2010 Change $pos = strpos($friend_content, ($_POST['fcode']); to: $pos = strpos($friend_content, $_POST['fcode']); There seems to have been an error when the code was written, that is line 7. ahh thanks now it works perfectly!! thanks so much every one!! I will probably have more questions down the road but for now i'm good. I <3 u all Quote Link to comment https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988546 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.