Reputation: 1
Hello my friends I am fairly new in javascript and html as well so this project is taking much longer than I probably need. Here is the problem, I want to save the contents of a table into a text file.
Here is the code in the form
print start_form(-action=>"",-onSubmit=>"return false;");
print table({-border=>1,-cellpadding=>3,-name=>"tabla",-id=>"tab"},
th(["Matricula","Nombre","Apellido Paterno","Apellido Materno","Sexo","Edad","Carrera","Email",
"Materias","Promedio","Borrar"]));
print submit(-label=>'Guardar',-onClick=>'salvar();',-onSubmit=>"salvar(); return false;");
print hidden(-name=>'escondido',-id=>'hid',
-default=>['0']);
print hidden(-name=>'escondido2',-id=>'hid2',
-default=>['nadiemeve2']);
print end_form;
Rows are added to the table as it reads a file using javascript, so when I want to save the contents of the displayed table into a text file I do this:
if (param) {
open FT, "+>>".param('escondido2') or die "No se puede abrir el archivo";
print FT param('celda60');
close FT;
}
Here, 'celda60' is the name of a single textfield inside a field, the name is given as the cell is created. As you might notice, I am just trying to save a single cell as a test, the thing is, that it doesn't work. It doesnt save a thing. but if I do: print FT param('celda60')."TEST"; the only thing it saves in my file is TESTESTESTEST, so the problem gotta be param('celda60') In case you wonder 'salvar()' looks like this
function salvar(){
var table2 = document.getElementById('tab');
var rowCount2 = table2.rows.length - 1;
document.getElementById('hid').value=rowCount2; }
the only thing it does is to get the amount of rows so when I get the saving thing going I can save the content inside each textfield from each cell by doing a few cycles.
Any idea of what I am doing wrong here? I mean, besides lots of rudimentary stuff I guess.
Upvotes: 0
Views: 358
Reputation: 27193
Maybe you are overcomplicating your question, something like this will allow you to have a user enter a list of values to a form and then save those values to a data file on the server.
use strict;
use warnings;
use CGI;
use IO::File;
my @FIELDS = qw(
matricula nombre
apellido_paterno apellido_materno
sexo edad
carrera email
materias promedio
borrar
);
i f( param('gotstuff') ) {
my $fh = IO::File->open( 'datafile.txt', '>>' );
my @data = param( @FIELDS );
$fh->print join ',', @data;
# Print a thank you for your data page here.
}
else {
# Do your other junk here, start some html, etc
# print the form and so on.
print_form();
# Print the rest of your HTML here.
}
sub print_form {
my @fields = qw(
matricula nombre
apellido_paterno apellido_materno
sexo edad
carrera email
materias promedio
borrar
);
my @headings = map {my $w = $_; $w=~/_/ /g; uc_first $w } @fields;
print
start_form(-action => script_name(), -method => 'POST' ),
table({-border=>1,-cellpadding=>3,-name=>"tabla",-id=>"tab"},
Tr([
th(\@headings),
td([ map textfield($_), @fields ]),
]),
),
submit( -label => 'Guardar',
-onClick => 'salvar();',
-onSubmit => 'salvar(); return false;',
),
hidden( 'gotstuff', 'gotstuff' ),
end_form();
}
Upvotes: 2
Reputation: 27193
You need to read a well written Perl and CGI tutorial for sometime after 1998.
I recommend Ovid's CGI Course. It's a venerable classic, but it is very well written and does not encourage bad practices.
You've got at least one place where I can run arbitrary commands using shell escapes in my CGI arguments. That is why practices like tainting user data and 3 argument open commands are standard practices and have been for many years.
General advice:
strict
and warnings
.open
with lexical handles, or just use IO::File. open my $fh, '>>', $pathtofile or die "Ouch $!";
print
can handle a list of arguments. There is no need to have 50 print statements in your code.On the last couple points here's an example:
sub print_form {
print
start_form(-action=>"", -onSubmit=>"return false;"),
table({-border=>1,-cellpadding=>3,-name=>"tabla",-id=>"tab"},
th([ "Matricula", "Nombre",
"Apellido Paterno", "Apellido Materno",
"Sexo", "Edad",
"Carrera", "Email",
"Materias", "Promedio",
"Borrar",
])
),
submit( -label => 'Guardar',
-onClick => 'salvar();',
-onSubmit => 'salvar(); return false;',
),
hidden( -name=>'escondido', -id=>'hid', -default=>['0'] ),
hidden( -name=>'escondido2', -id=>'hid2', -default=>['nadiemeve2'] ),
end_form();
}
Although, in my code I'd make the html and pass it back. I like to keep side effects, like printing stuff out, closely grouped.
Upvotes: 2
Reputation: 39158
Client side modifications of an HTML document are not automatically reflected via CGI into the state of the server-side program. You need an additional mechanism, e.g. AJAX.
Upvotes: 3