| |||||||||||
| |||||||||||
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.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |