Textbox saves to txtfile

All about creating websites!
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8069
Joined: 27 Aug 2005, 16:00
15
Contact:

Textbox saves to txtfile

Post by ayu »

I have been trying over and over again...but nothing works

i have tried to make 2 textboxes with 1 submit button.

when a user enters a message and name in the 2 textboxes and clicks submit the text is saved to a txtfile (ex: log.txt)

can't get anything to work. can anyone help? :)

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11575
Joined: 06 Apr 2005, 16:00
16
Location: The zone.
Contact:

Post by bad_brain »

well, this can´t be done with html alone, you will need support from a script language like php or Perl, because with html you don´t have the possibility to create new files. Here´s how I would do it in Perl:

1. create the database where the data will be stored:

Code: Select all

#C:/perl/bin/perl -w
use DBI;
my $dbh = DBI->connect("DBI:CSV:f_dir=c:/database")
or die "Couldn´t connect to database!\n$!";

my $sth=$dbh->prepare ('CREATE TABLE db_mitarbeiter
(Nachname char(30),
Vorname char(30),
Abteilung char(30),
Telefon integer)')
or die "Couldn´t establish database!\n$!";

$sth->execute();
$sth->finish();

$dbh->disconnect;
this will create a database with 4 fields named db_mitarbeiter (sorry for the german variable names). the fields are Nachname (last name), Vorname (first name), Abteilung (department) and Telefon (phone number).

2. the html interface
a.) the form for search queries:

Code: Select all

<html>
<head>
<title>Entry form</title>
</head>
<body>
<h1><center>Adressbook</center></h1>

<FORM Action="../cgi-bin/database.pl" Method="post">
<hr>
Please enter <b>both</b> search queries:
<table>
<tr>
<td>Nachname:</td>
<td><INPUT TYPE="text" name="nachname"></td>
</tr>
<tr>
<td>Abteilung:</td>
<td><INPUT TYPE="text" name="abt"></td>
</tr>
</table>

<hr>
<INPUT TYPE="submit" name="suchanfrage" value="suche starten">
<INPUT TYPE="reset" value="Zur&uuml;cksetzen">
<hr>

<p>If you want to create a new entry<br>
please click <a href="formular2.html">here.</a>
</p>

</form>
</body>
</html>
b.) the form for new entries:

Code: Select all

<html>
<head>
<title>Entry form</title>
</head>
<body>

<h1><center>New entry</center></h1>
<FORM Action="../cgi-bin/database.pl" Method="post">

<hr>
Enter the new data (please use all fields):

<table>
<tr>
<td>Vorname:</td>
<td><INPUT TYPE="text" name="vorname"></td>
</tr>

<tr>
<td>Nachname:</td>
<td><INPUT TYPE="text" name="nachname"></td>
</tr>

<tr>
<td>Telefonnummer:</td>
<td><INPUT TYPE="text" name="telefon"></td>
</tr>

<tr>
<td>Abteilung:</td>
<td><INPUT TYPE="text" name="abt"></td>
</tr>
</table>

<hr>

<INPUT TYPE="submit" name="eintrag" value="Datensatz eintragen">
<INPUT TYPE="reset" value="Zur&uuml;cksetzen">

<hr>

<p>To the <a href="formular1.html">search function.</a></p>

</form>

</body>
</html>
3. the CGI script which connects the forms with the database:

Code: Select all

#!c:/perl/bin/perl -w

use strict;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;

my $DBH;
my $STH;
my $CGI;
my $antwort;

$CGI= new CGI();

###Search or new entry###

if ($CGI->param("suchanfrage")) {
suche ($CGI, $DBH, $STH);
}
elsif ($CGI->param("eintrag")) {
eintragen ($CGI, $DBH, $STH);
}

###Start Subroutine search###

sub suche {
my $nachname=param("nachname");
my $abteilung=param("abt");

$DBH = DBI->connect("DBI:CSV:db_mitarbeiter=d:/Server/xampp/cgi-bin")
or die "Couldn´t establish connection to database!$!";

$STH = $DBH->prepare("SELECT * FROM db_mitarbeiter WHERE Nachname='$nachname' AND Abteilung='$abteilung'")
or die "Couldn´t provide SQL query!$!";

$STH->execute()
or die "Can´t execute:$!";

###Hit###

if ($STH->rows !=0) {
print <<HERE_TEXT1;
Content-Type:text/html

<html>
<head>
<title>Answer</title>
</head>
<body>
<center>

<h1>Search results:</h1>
<hr>

<table border>
<tr>
<td width="200"><b>Familienname:</b></td>
<td width="100"><b>Vorname:</b></td>
<td width="200"><b>Abteilung:</b></td>
<td width="100"><b>Telefon:</b></td>
</tr>

HERE_TEXT1

my @data;
while (@data=$STH->fetchrow_array()) {
my $familienname=$data[0];
my $vorname=$data[1];
my $abt=$data[2];
my $telefon=$data[3];

print qq§<tr>\n<td><b>$familienname</b></td>\n<td>
$vorname</td>\n<td>$abt</td>\n<td>$telefon</td>\n</tr>\n§;
}
print qq§</table>\n<p><a href=\"../htdocs/formular1.html\">New search!</a></p>\n</html>§;
}

###No hit###

else {
print <<HERE_TEXT2;
Content-type:text/html

<html>
<head>
<title>Antwort</title>
</head>
<body>

<p>No results for '$nachname'!</p>

<hr>

<p><a href=\"../htdocs/formular1.html\">New search!</a></p>

</body>
</html>

HERE_TEXT2
}
$STH->finish();
$DBH->disconnect;}
###Ende Subroutine Suche###

###Start Subroutine new entry###

sub eintragen {

my ($nachname, $vorname, $abt, $telefon)= (param('nachname'),
param('vorname'), param('abt'), param('telefon'));

$DBH=DBI->connect("DBI:CSV:db_mitarbeiter=d:/Server/xampp/cgi-bin")
or die "Couldn´t establish connection to database!$!";

$STH=$DBH->do("INSERT INTO db_mitarbeiter VALUES ('$nachname','$vorname','$abt','$telefon')")
or die "Couldn´t provide SQL query!$!";

print <<HERE_TEXT3;
Content-type:text/html

<html>
<head>
<title>Antwort</title>
</head>

<body>

<h4>Database entry:</h4>
<hr>
<p><b>$nachname</b>, $vorname, $abt, $telefon</p>
<hr>

</body>
</html>

HERE_TEXT3

$DBH->disconnect;

}
#Ende Subroutine new entry
well, you would have to change some paths in the scripts and download some modules for Perl like DBI and DBI-CSV to use it, but maybe it helps you to see how it works.
if it´s a little too much for you you can surely find some nice premade stuff on http://www.hotscripts.com , check php and Perl first because most servers support these languages, guestbook scripts would be right for you, because what you want is actually some kind of "invisible" guestbook.
To be able to test Perl- or php-pages you need to put them on a server, I suggest xampp, it´s a fully functional apache server with php, Perl and MySQL. It´s really easy to install and use: http://www.apachefriends.org/en/xampp.html

For serious development in Perl it´s inevitable to use a full Perl environment (the xampp one is just very basic), get it here:
http://www.activestate.com/Products/ActivePerl/
It´s free and you can fake all entries in the form...

Hope it helped a bit...:wink:

User avatar
ayu
Staff
Staff
Posts: 8069
Joined: 27 Aug 2005, 16:00
15
Contact:

Post by ayu »

woaw....dude thats alot of help.....really thanks :D i will try it out....just one more thing....is there anyway to just have a html document that is connected to a PHP file that saves the text? in a small easy code? =) like save it to a log.txt? =) had that b4...but i formated the computer and i forgot to backup those files ^^

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11575
Joined: 06 Apr 2005, 16:00
16
Location: The zone.
Contact:

Post by bad_brain »

um, I have to admit I´m not really into php, but it can be done with Perl too:

Code: Select all

#!/usr/bin/perl
use warnings FATAL => 'all';
use strict;
use CGI qw(param);

open F, ">file.txt";
print F param('textarea-name');
close F;
this script saves all entries from the textarea named textarea-name to a txt-file named file.txt. you just have to place it in the cgi-bin of the server and add the path to cgi-bin in the html-form, like

Code: Select all

<FORM action="cgi-bin/scriptname.pl" METHOD="post">
the submitted form entry then will be directed to the perl script which saves it to the txt-file (make sure the name of the textfield matches with the name in the ('textarea-name')-parameter of the script)... :wink:

User avatar
ayu
Staff
Staff
Posts: 8069
Joined: 27 Aug 2005, 16:00
15
Contact:

Post by ayu »

haha thanks :D i will try it out

Post Reply