write a form to a text file

02-22-07, 05:22 AM
|
|
Newbie Coder
|
|
Join Date: Feb 2006
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
write a form to a text file
Hey guys I desperately need some help. I'm supposed to have completed a petition but forgot about it, so thought I'd set up one online really quickly.
I just need some help, I'm not sure how to submit the fields o a form to a simple text file. Please can someone yhelp me with this, thank you
|

02-28-07, 04:42 PM
|
|
New Member
|
|
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by eviljoker7075
Hey guys I desperately need some help. I'm supposed to have completed a petition but forgot about it, so thought I'd set up one online really quickly.
I just need some help, I'm not sure how to submit the fields o a form to a simple text file. Please can someone yhelp me with this, thank you
|
i need the same thing
|

02-28-07, 05:36 PM
|
 |
Junior Code Guru
|
|
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well,
First you will need a CGI (Perl/php) to parse form data. Google will be your friend.
Next output form data to a file:
Perl Based code.
open(OUT,">formdata.txt");
print OUT "FNAME $FORMDATA[0]\tLNAME $FORMDATA[1]\tEMAIL $FORMDATA[2]\n";
print "Thanks for submitting our form.<p>";
close OUT;
|

02-28-07, 06:01 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,898
Thanks: 0
Thanked 63 Times in 61 Posts
|
|
PHP Code:
// PHP Based code.
$open_file = fopen("formdata.txt", "w"); // Open file for writing. //
fwrite($open_file, $fname."\n");
fwrite($open_file, $lname."\n");
fwrite($open_file, $email."\n");
echo( "Thanks for submitting our form.<p>");
fclose($open_file);
__________________
Jerry Broughton
|

02-28-07, 06:21 PM
|
 |
Community Leader
|
|
Join Date: Sep 2005
Location: Spain
Posts: 7,648
Thanks: 6
Thanked 33 Times in 30 Posts
|
|
Note that the previously submitted data gets erased when writing to the log file which has been opened in "w" mode. Replace the "w" with "a", to add the new data below the old data.
http://www.php.net/fopen
|

02-28-07, 07:13 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,898
Thanks: 0
Thanked 63 Times in 61 Posts
|
|
When I used to use flat files, I couldn't get the append mode to work properly. So
I always read my file into an array then added my new data to the array and then wrote the array back to the file using the write mode.
__________________
Jerry Broughton
|

03-02-07, 04:15 AM
|
|
New Member
|
|
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Jerry,
Not sure where to place that PHP code. Does that code go in its own file? If so, do you set the file name as a form action ?
Yes I'm VERY new to PHP, sorry to be so totally clueless. Thanks for your help.
JR
|

03-02-07, 06:42 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,898
Thanks: 0
Thanked 63 Times in 61 Posts
|
|
If you want to program even the basic functions that you will need on the internet today,
it becomes necessary to learn HTML, CSS, javascript & PHP.
HTML is used to organize and display your data.
CSS is used to format & organize your data.
Javascript is used to manipulate the objects and elements on the screen(it can also do anything that HTML and CSS can do),
and PHP is used for communicating with files & databases, manipulating arrays, sending email and a whole lot more.
So if you already have a basic understanding of HTML, than you are half way there to learning PHP.
PHP is used in the same document that your HTML is in.
You just need to learn how to talk to files with PHP.
The code I presented is the basic structure for creating and writing to a file.
The only other concideration for now is: HTML files usually have a file extention of .html or .htm, but when you add in PHP then you change the extention to .php that way the script interpreters know that you are programming in PHP.
__________________
Jerry Broughton
|

03-03-07, 08:19 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,898
Thanks: 0
Thanked 63 Times in 61 Posts
|
|
Oh, yes, I neglected to mention one thing... Make sure you enclose your PHP code in PHP tags, like this:
PHP Code:
<?php
echo("Hello");
?>
Some people like to take short-cuts with the PHP tags and use them this way:
PHP Code:
<?
echo("Hello");
?>
I don't take that short-cut, because it doesn't always work the way you think it should.
__________________
Jerry Broughton
Last edited by job0107; 03-03-07 at 08:22 PM.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|