Windchill
06-03-2003, 07:44 AM
Lets say I have a file called output.txt and I want to write a string from a form into it. How would I do this:confused:
|
Click to See Complete Forum and Search --> : How do you write form input to a file? Windchill 06-03-2003, 07:44 AM Lets say I have a file called output.txt and I want to write a string from a form into it. How would I do this:confused: pyro 06-03-2003, 08:05 AM Try something like this... <?PHP $filename = "output.txt"; #Must CHMOD to 666 $text = $_POST['theformfieldname']; # Form must use POST. if it uses GET, use the line below: #$text = $_GET['theformfieldname']; #POST is the preferred method $fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents if ($fp) { fwrite ($fp, $text); fclose ($fp); echo ("File written"); } else { echo ("File was not written"); } ?> Windchill 06-03-2003, 08:09 AM Whoah! I love your 2.3 minute response time :D . Can I creat files if the folder I'm creating them to is CHMODed to 666 using the same method? pyro 06-03-2003, 08:22 AM For the folder, you will want to CHMOD to 777 rather than 666... Windchill 06-03-2003, 08:24 AM Great. Thanks(again). Do you get paid for this or something? So I make a HTML document that has a form that sends it to this ^ php file? I know I'm a hopeless newbe when it comes to forms and PHP. pyro 06-03-2003, 08:26 AM lol... Don't I wish... :D Nope, all the Moderators are just volunteers. pyro 06-03-2003, 08:29 AM Yeah, but that is only going to work for one form field. If you need more, we will need a foreach loop, or something similar, to loop through all the $_POST variables... Windchill 06-03-2003, 08:32 AM OK so I do it twice, once for the first form and once for the second. Great. Thanks a lot :) pyro 06-03-2003, 08:50 AM If you are interested, I wrote this up quick... It will loop through and add all the form elements to a .txt file. Form's method must be POST... <?PHP ###################### Set up the following variables ###################### # # $filename = "output.txt"; #CHMOD to 666 $forward = 0; # redirect? 1 : yes || 0 : no $location = "thankyou.htm"; #set page to redirect to, if 1 is above # # ##################### No need to edit below this line ###################### ## set time up ## $date = date ("l, F jS, Y"); $time = date ("h:i A"); ## mail message ## $msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n"; foreach ($_POST as $key => $value) { $msg .= ucfirst ($key) ." : ". $value . "\n"; } $msg .= "-----------\n\n"; $fp = fopen ($filename, "a"); # w = write to the file only, create file if it does not exist, discard existing contents if ($fp) { fwrite ($fp, $msg); fclose ($fp); } else { $forward = 2; } if ($forward == 1) { header ("Location:$location"); } else if ($forward == 0) { echo ("Thank you for submitting our form. We will get back to you as soon as possible."); } else { "Error processing form. Please contact the webmaster"; } ?> UniqueCreations 04-11-2008, 07:47 AM Hello all! I am new to this forum and haven't had much time to look around... yet. I stumbled upon this ancient post via google search and it is exactly what I want to do. However, I need some different results. Let me explain. I have made a site for a church. I want to be able to allow the pastor to update the events page. At the moment I am unsure what hosting options his site will have as he does not have the information available at the moment. So in order to avoid any problems with available MySQL options I decided to make a password protected form page that he can log into. After filling out the form information I am going to use a similar PHP script as posted above to write the information to a text file. Then via dreamweaver I have it set to call upon the text file using SSI to display the event information. The only problem thus far is formatting this text information. As it is now, it writes everthing with the Input ID in front of it. How would I go about formatting the text to display with broper line breaks and possible H1 tags? I realize this might not be the best method to use, but I would still like to get it to work. I am also open to suggestions to achieve the desired results. Ideally I want the pastor to be able to input multiple events, anywhere from 1 to 5 depending on what he has upcoming. The form that I set up has sections for What, Where, When, Comments all duplicated three times to allow three events. emck 11-15-2009, 06:25 AM I just signed up to weDeveloper.com and I am new at PHP. I have been using the flowing code to write the form output to a text file and i would like to also have it send me an email. I would much apreciat if someone could let me know how to modify the code. thanks <?PHP ###################### Set up the following variables ###################### # # $filename = "output.txt"; #CHMOD to 666 $forward = 0; # redirect? 1 : yes || 0 : no $location = "thankyou.htm"; #set page to redirect to, if 1 is above # # ##################### No need to edit below this line ###################### ## set time up ## $date = date ("l, F jS, Y"); $time = date ("h:i A"); ## mail message ## $msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n"; foreach ($_POST as $key => $value) { $msg .= ucfirst ($key) ." : ". $value . "\n"; } $msg .= "-----------\n\n"; $fp = fopen ($filename, "a"); # w = write to the file only, create file if it does not exist, discard existing contents if ($fp) { fwrite ($fp, $msg); fclose ($fp); } else { $forward = 2; } if ($forward == 1) { header ("Location:$location"); } else if ($forward == 0) { echo ("Thank you for submitting our form. We will get back to you as soon as possible."); } else { "Error processing form. Please contact the webmaster"; } ?> webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |