Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Writing form field names to text file

Posted by Anji (Anji), 24 July 2002
I have a form on my web page and it is sending its form field contents to a text file on the server.  However, when the text file is a blank one, I first want to send the names of the form fields to the file, separated by tabs.  I have the code to test for the file size but have been unable to work out how to send the names of the form fields into the file.  Can anyone help with the code for this?  You're wonderful if you can!

Posted by admin (Graham Ellis), 25 July 2002
I think the "vital line" for you would be:
     fputs ($fp,implode("\t",array_keys($_POST)));

All your form fields are held in the $_POST array (that's from PHP release 4.1, and using the POST method; if you want to be compatible with earlier versions, use $HTTP_POST_VARS instead, and if you're using the GET method, use $_GET).

array_keys retreives all the keys and puts them into an indexed array, implode puts the tabs between them, fputs outputs them ....

Alternatives ... you could also write loops using array_walk or each, but they would make for longer coding,  and you would have to take care to ensure that you didn't have a trailing tab on your line end.

Posted by bschultz (bschultz), 28 February 2003
The following code writes the form input to the text file, but the field names are not there:


$out = fopen("results.txt", "a");

if (!$out) {
print("Could not append to file");

$T1 = $_POST["T1"];
$T2 = $_POST["T2"];
$T3 = $_POST["T3"];

fputs ($fp,implode("\t",array_keys($_POST)));




Here's the error message I get:

Warning: fputs(): supplied argument is not a valid stream resource in c:\program files\apache group\apache\htdocs\phptest\stats.php on line 15

ANy ideas?

Thanks, as always!

Posted by admin (Graham Ellis), 1 March 2003
you seem to have opened your file on $out, then written to $fp - make them both $out, or both $fp, and it should work.   By the way, I'm assuming that $fp isn't already opened in another area of your php code that you havebn't shared  

Posted by bschultz (bschultz), 1 March 2003
Thanks Graham, that worked...kind of.

It put all the field names first, separtated by tabs, followed by the text inputed into the field after.

For example:

field name 1     field name 2    field name 3    field name 4

field name 5   txt for field 1    txt for field 2   txt for field 3

txt for field 4   txt for field 5

Is there any way to have it format like this:

field name 1  | txt for field 1
field name 2  | txt for field 2
field name 3  | txt for field 3
field name 4  | txt for field 4
field name 5  | txt for field 5


Posted by admin (Graham Ellis), 3 March 2003
To loop through each element of an array, listing out keys and values, you can use a loop of calls to each, or a foreach loop, or an array_walk - this is one of the areas that PHP sometimes seems to be too flexible for its own good

Here's some sample code that takes all the elements of a form (I happen to have used the GET method, but all will work for post) and displays them all using each of the three methods.   If you want the output sorted, use asort or ksort before the loop.

<head><title>This is a demo of array loops</title></head>
<body bgcolor=white>
Please enter some information <input name=some><br>
And some more <input name=more><br>
And still more <input name=still><br>
<input type=hidden name=hush value="further results">
<input type=submit name=go value="Do it!">
<hr>From your previous form:

foreach (array_keys($_GET) as $k) {
       print ("$k ..... $_GET[$k] .... (1) <br>");

reset ($_GET);
while  (list ($k,$v) = each ($_GET)) {
       print ("$k ..... $v .... (2) <br>");

function prfunc ($ky,$va) {
       print ("$ky ..... $va .... (3) <br>");
Sometimes there are just too many ways!<br>
- Graham

Posted by crazyjake (crazyjake), 10 March 2004
You see the code here:

Please could you tell me how to make a form so it will submit it to the txt file!



$out = fopen("results.txt", "a");

if (!$out) {
print("Could not append to file");

$T1 = $_POST["T1"];
$T2 = $_POST["T2"];
$T3 = $_POST["T3"];

fputs ($out,implode("\t",array_keys($_POST)));  




Would this be a form that i could use that i previosly made?

<FORM ACTION="HandleForm.php">
First Name <INPUT TYPE=TEXT NAME="FirstName" SIZE=20><BR>
Last Name <INPUT TYPE=TEXT NAME="LastName" SIZE=40><BR>
E-mail Address <INPUT TYPE=TEXT NAME="Email" SIZE=60><BR>
Comments <TEXTAREA NAME="Comments" ROWS=5 COLS=40></TEXTAREA><BR>

Posted by admin (Graham Ellis), 10 March 2004
Err - I don't totally follow the question here. Your sameple form ends in ".php" so it should submit to that.  Are you saying you want to submit to a .txt file (i.e. have .txt files parsed as if they're php?).  You could do this by adding a line to your .htaccess file but I'm not sure why you would.   By the way - we do something like this to have all our .html files go through PHP

Posted by crazyjake (crazyjake), 11 March 2004

I got my answer from another thread anyway now cheers thou!

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: • WEB: • SKYPE: wellho