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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Sending form variables to a text file

Posted by Anji (Anji), 30 July 2002
The behaviour of text fields when sending field names to a file is different to that of checkbox fields.  Text fields are easy.  I am struggling to send both the names of the checkbox fields and the value of the fields to the text file, being able to send one or the other but not both together.  Also, if the compulsory fields were not completed by the user and the form has to be redisplayed, sending the field names and values behaves differently yet again.  Any suggestions?

Posted by admin (Graham Ellis), 1 August 2002
Indeed the formats of the different field types are different - you need to "thank" the originatiors of HTML forms for that (although they did a pretty good job considering they didn't realise how big this form thing was going to turn out to be!

I've put up a test form with a text field, a chack box, and a selection menu for good measure at:
http://www.wellho.net/formels.php4
I've made all the fields "sticky" - in other words, once you fill in a box it will be echoed back that way on the next form.  All entries are simply appended to a log file at the moment, and that log file is echoed back after the form.

Here's the source code:

<html>
<head><title>Form Elements to file</title></head>
<body bgcolor=white text=blue>
<H1>Sample form - save fields to file</h1>
<?php

// This is a form with "sticky" fields .....

?>
<form method=post>
Text Box <input name=one
  value="<?php
  echo (htmlspecialchars(stripslashes($HTTP_POST_VARS["one"])));
  ?>" size=15><br>
Checkbox <input name=two type=checkbox
  <?php
  if ($HTTP_POST_VARS["two"] != "") echo ("checked");
  ?>><br>
Select <select name=three>
  <?php
  $compass = array ("North","South","East","West") ;
  foreach ($compass as $possibility) {
     echo ("<option");
     if ($HTTP_POST_VARS["three"] == $possibility) echo (" selected");
     echo (">$possibility");
  }
  ?></select><br>
<input type=submit>
</form>
<?php

// Save all the fields on any previous form to a file

$fh = fopen("../testdata","a");
fputs($fh,$HTTP_POST_VARS["one"]." - ".
  $HTTP_POST_VARS["two"]." - ".
  $HTTP_POST_VARS["three"]." - ".time().
  "\n");
fclose($fh);

// Echo the file contents onto the end of the form

$stuff = implode("",file("../testdata"));
echo (nl2br(htmlspecialchars(stripslashes($stuff))));

?>

</body>
</html>



This page is a thread posted to the opentalk forum at www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho