c_pattle Posted February 27, 2012 Share Posted February 27, 2012 I have the following form the just has one field. <html> <head> <title>Post</title> </head> <body> <form action="worldpay_callback.php" method="post"> <input type="text" name="username" id="username" /> <input type="submit" name="submit" id="submit" value="send" /> </form> </body> </html> I am then posting the form to the following page that is meant to loop through all of the post values and save them to a text file. For some reason my foreach loop doesn't seem to be working as done of the post values are getting written to the text file. Can anyone see anything I'm doing wrong? $myFile ="notifications.txt"; $fh = fopen($myFile, 'w+') or die("can't open file"); foreach($_POST as $k=>$v) { $data = 'POST: Key:'.$k.' - Value:'.$v ." \n"; fwrite($fh, $data); } fclose($fh); return "[OK]"; Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/257858-foreach-loop-not-working/ Share on other sites More sharing options...
c_pattle Posted February 27, 2012 Author Share Posted February 27, 2012 Also I've made sure that its not a permissions problem by giving the text file 777 permissions Quote Link to comment https://forums.phpfreaks.com/topic/257858-foreach-loop-not-working/#findComment-1321616 Share on other sites More sharing options...
AyKay47 Posted February 27, 2012 Share Posted February 27, 2012 The highest permissions a file should ever have are 0755. Why do you need a loop if only one value is being sent to the server? You do realize that opening a file in "w+" mode will truncate the file every time correct? You should have error_reporting set to E_ALL or -1, and Display_errors set to "On", if it is a file permissions error, an error will be triggered. A couple debugging precautions should be put in place here: if(isset($_POST['submit'])) { $myFile ="notifications.txt"; $username = trim($_POST['username']); $fh = fopen($myFile, 'w+') or die("can't open file"); $data = 'POST: Key: ' . $k . ' - Value: ' . $v . " \n"; $fw = fwrite($fh, $data); if($fw === false) { echo "failed to write to file."; } fclose($fh); } Quote Link to comment https://forums.phpfreaks.com/topic/257858-foreach-loop-not-working/#findComment-1321658 Share on other sites More sharing options...
PFMaBiSmAd Posted February 27, 2012 Share Posted February 27, 2012 Something tells me that is not all of your php code. Have you even checked if the code is being executed by echoing something in it? What's all the actual php code in your file, including the opening php tags you are using? Quote Link to comment https://forums.phpfreaks.com/topic/257858-foreach-loop-not-working/#findComment-1321659 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.