Bentley4 Posted May 1, 2011 Share Posted May 1, 2011 Hello! I'd like to know how one can save inputted data using a form to store it into another php file. The user should just input their name in the php webpage called "MainPage.php". When they write their names and hit submit, they would just stay on the same page. Their submitted names should be stored permanently into a "Answers.php" file. What do I need to change in my "MainPage.php code? <html> <body> <form method="post" action="Answers.php" > Name:<input type="text" name="Name" /> <input type="submit" value="Submit name" /> </form> [some php code not relevant to question] </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/ Share on other sites More sharing options...
Fadion Posted May 1, 2011 Share Posted May 1, 2011 You mean that they fill in their name in MainPage.php and it shows up on Answers.php? Whatever that case would be, there's no permanent storage unless you go for a database approach. The options you have, appart a database, are sessions and cookies. Answers.php <?php session_start(); //initializes sessions and should be place at the top of the document if (isset($_POST['name'])) { $_SESSION['name'] = htmlentities($_POST['name']); echo $_SESSION['name']; } ?> Once $_SESSION['name'] has been set, it can be called from any page you like, be it Answers.php, Questions.php or even Ducks.php Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/#findComment-1209002 Share on other sites More sharing options...
Bentley4 Posted May 4, 2011 Author Share Posted May 4, 2011 Thank you for your response guilty gear. I just found a blog that claims it is possible and explains how to save web form data through using this php code: <?php $name = $_POST['name']; $email = $_POST['email']; $fp = fopen(”formdata.txt”, “a”); $savestring = $name . “,” . $email . “n”; fwrite($fp, $savestring); fclose($fp); echo “<h1>You data has been saved in a text file!</h1>”; ?> From tutorial: http://www.howtoplaza.com/save-web-form-data-text-file 1. Why does the the person who wrote the tutorial save this php code into a seperate php file? 2. When I use this code in my php file of my webpage I get this error: Parse error: syntax error, unexpected ',' And it refers to this line: $savestring = $name . “,” . $email . “n”; Why? Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/#findComment-1210302 Share on other sites More sharing options...
spiderwell Posted May 4, 2011 Share Posted May 4, 2011 i was about to suggest writing to text file, seems you already found it! Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/#findComment-1210305 Share on other sites More sharing options...
Bentley4 Posted May 4, 2011 Author Share Posted May 4, 2011 No, the problem isn't solved! I did everything according to that tutorial: http://www.howtoplaza.com/save-web-form-data-text-file 1. I Implemented that first piece of code in my main webpage php file. 2. Made a "formdata.txt" file, inputted 'Name,Mail,[enter]' and saved it in my webfolder. 3. Then made a “process-form-data.php” in my webfolder and inserted this code: <?php $name = $_POST['name']; $email = $_POST['email']; $fp = fopen(”formdata.txt”, “a”); [color=blue]$savestring = $name . “,” . $email . “n”;[/color] fwrite($fp, $savestring); fclose($fp); [color=blue]echo “<h1>You data has been saved in a text file!</h1>”;[/color] ?> When I check the webpage of “process-form-data.php” it says Parse error: syntax error, unexpected ',' in XXXX/process-form-data.php on line 7 Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in XXXX/process-form-data.php on line 10 Why? I don't see any concatenation errors in line 7 nor any mistakes in line 10 Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/#findComment-1210336 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 The error is caused by the curly quotes used throughout the code. If you change everything to straight quotes, it should work just fine. <?php $name = $_POST['name']; $email = $_POST['email']; $fp = fopen("formdata.txt", "a"); $savestring = $name . "," . $email . "n"; fwrite($fp, $savestring); fclose($fp); echo "<h1>You data has been saved in a text file!</h1>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/#findComment-1210339 Share on other sites More sharing options...
Bentley4 Posted May 4, 2011 Author Share Posted May 4, 2011 Wow, nice one cyberRobot! Impressed that you spotted that. Thank you, it works! Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/#findComment-1210414 Share on other sites More sharing options...
cyberRobot Posted May 4, 2011 Share Posted May 4, 2011 No problem, glad to help! I didn't realize the curly quotes would cause problems with code until today. So we both learned something. Of course this means I need to modify my own blog posts which contain code; WordPress loves those curly quotes. Quote Link to comment https://forums.phpfreaks.com/topic/235265-store-data-into-php-file-without-being-directed-to-that-page/#findComment-1210420 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.