Writing form data to file |
Writing form data to file |
tudsy |
Nov 17 2014, 04:12 AM
Post
#1
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
Hi
Just a little problem. I am designing a survey With a few pages. The problem is that I trying to write form data to a file. Can anyone help? |
CharlesEF |
Nov 17 2014, 07:07 AM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
How far have you gotten? Post some code and maybe someone can help.
|
Christian J |
Nov 17 2014, 08:22 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
If you use PHP, see the file_put_contents function in the PHP manual.
|
tudsy |
Nov 18 2014, 07:45 AM
Post
#4
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
How far have you gotten? Post some code and maybe someone can help. survey.html ( 1.03k ) Number of downloads: 377 page4.php ( 5.64k ) Number of downloads: 333 page3.php ( 5.51k ) Number of downloads: 370 page2.php ( 1.73k ) Number of downloads: 385 page1.php ( 1.46k ) Number of downloads: 386 [att achmentid=2253] end.php ( 1.16k ) Number of downloads: 424 Hi Here is some code. Attached File(s) Intro.php ( 2.03k ) Number of downloads: 149 |
CharlesEF |
Nov 18 2014, 08:21 PM
Post
#5
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
You still have a ways to go before you start writing data to a file. Your logic still has problems. The way you have coded PHP you are writing data to the file before the information is filled in. In fact, page1.php does not write anything at all because I have no idea where this value is coming from:
CODE fwrite($fp,"$_POST[name]\t"); You do not close the data file on any of the pages. Your <form action=""> is wrong because you do not format it as PHP code. Example: CODE page2.php?$_SERVER[QUERY_STRING'] should be written as:CODE page2.php?<?php echo($_SERVER['QUERY_STRING']);?> Your Intro.php page should not create a file until you click the 'Next' button, not when the page loads. If the user clicks the refresh icon then a new file will be created every time. And I'm not even looking at the HTML errors, yet. In my opinion, each page should post to itself. Then your PHP code checks to see if the page has been submitted, if it has, then you process the data for that page. The script continues with a page redirect to the next page. Example: CODE header('Location: ./page2.php'); And I would not use $_SERVER['QUERY_STRING'] at all. I would pass the filename from page to page as a POST value and then use it as either a PHP variable or an HTML hidden field to hold the filename. |
CharlesEF |
Nov 19 2014, 12:32 AM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
After thinking about this for a while I would suggest you save the data you want in a session variable. As you progress through each page add another session variable to store the information for that page. Then at the end of the survey you combine all the session variables into a string and use 'file_put_contents' to write the data into a file. If you write the data after each page and the user decides not to finish the survey then you will not be left with a lot of partly finished files hanging around.
Or, if you don't want to use session variables, you could use hidden input text fields to store the data as you progress thru each page. These hidden fields would need to be placed in the <form></form> so the values get posted and the next page can read them. So, page1.php would contain no hidden fields, page2.php would contain a hidden field called age to store the age value, page3.php would contain a hidden field called age and gender, page4.php would contain hidden fields age, gender and something (I can't tell what you are trying to do, you have duplicated so much code, on page3.php and page4.php) and when end.php loads that is when you generate an id, collect the POST values for your fields into a string and write the data to a file. This should be done before the user ends the survey because you can't count on the user clicking the 'End Survey' button. And you have the extra bonus of posting to the next page by using the 'action=' attribute. This post has been edited by CharlesEF: Nov 19 2014, 01:19 AM |
CharlesEF |
Nov 19 2014, 06:19 AM
Post
#7
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
One thing I should mention. If you want to validate the user input before you proceed to the next page then each page would have to post to itself. This way you can make sure the user entered some data. Your PHP code would then redirect to the next page but this means you would have to save all user data in session variables, because the redirect would wipe out the $_POST values.
|
tudsy |
Nov 19 2014, 12:54 PM
Post
#8
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
Thanks
|
tudsy |
Nov 24 2014, 05:38 AM
Post
#9
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
Hi
Thanks for that. I have decided to use SESSION values to store and then write the data to file. As a result I am still having problems in writing to file. Also, when I press end survey (end.php), the final page is not my web page (http://ecovib2d.blogspot.com.au).. Can anyone help? page4.php ( 5.59k ) Number of downloads: 382 survey.html ( 1.03k ) Number of downloads: 321 page3.php ( 5.38k ) Number of downloads: 352 page2.php ( 1.67k ) Number of downloads: 359 page1.php ( 1.37k ) Number of downloads: 344 [att achmentid=2265] end.php ( 2.09k ) Number of downloads: 357 Attached File(s) Intro.php ( 1.36k ) Number of downloads: 161 |
Brian Chandler |
Nov 24 2014, 08:13 AM
Post
#10
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Charles said something like this before, but you just say "thank you", until you come back with the next problem -- except that really there is one Big Problem, which is (I think, correct me if I'm wrong) that you have not understood *at all* how server scripts work.
It is no good writing something like this in a PHP file: CODE ... html stuff ... <form ... form details, with inputs and a submit button ...> <?php // some program which looks as though it should handle the form ?. I guess that you hope that It goes through, and after It has done the form, It writes the results to a file. Trouble is, that there is no It. If you write php code in a file, and your server is set up to do it, when someone accesses your page, this php program will run, and will determine the content of the page sent to someone's browser. But this is all. Once the page is sent, no php program will run, anywhere. To handle form input, e.g. to write it to a file, you have to write a script which is accessed by the action parameter of the form. You can make the page "submit to itself" (by leaving the action parameter empty, I think, or by explicitly writing the name of the same file). In this case, though, the form input, when submitted, is present when the script is called, so normally you write the code for handling form input *before* you output the form itself. If the form submission was successful, it may not make sense to show the form again. Hope this help. Please do not just say "Thank you". |
CharlesEF |
Nov 25 2014, 05:59 PM
Post
#11
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Brian is correct, it seems you have yet to grasp how server-side scripts work. You still have many basic HTML errors in your pages, like, the opening <body> tag is missing from all your pages. On page4.php you use the same 'id' attribute name many times, you can only have 1 id per page. This means each id must be unique. On the same page you have a javascript function that is not called by anything on the javascript side, but you are trying to use the javascript variables in PHP code later down the line. You can't do that, unless you pass those variables when you submit the form. And you have this line of code:
CODE <input id="state_checked" type="checkbox" name="ssName" value="<?php if($ssAct == ('Next Page' || 'Previous Page') && $ssName!='') { echo '$ssName';} ?> "/> defined in 2 <td>'s, within each <td> 12 times, for a total of 24. Every line looks the same so why define it so many times? What do you think should happen with this code?I would recommend you start over with a different approach. To make your code easier to read you should remove any code not being used (like the javascript function OpenPage(url)). This post has been edited by CharlesEF: Nov 25 2014, 06:02 PM |
Lo-Fi Version | Time is now: 25th April 2024 - 01:31 AM |