简体   繁体   English

我无法将html表的内容保存到文本文件中。 使用CGI和PERL

[英]I can't save the contents of a table in html into a text file. Using CGI and PERL

Hello my friends I am fairly new in javascript and html as well so this project is taking much longer than I probably need. 您好,我的朋友们,我在javascript和html方面还相当陌生,因此该项目花费的时间比我可能需要的长得多。 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: 当它使用javascript读取文件时,会将行添加到表中,因此当我要将显示的表的内容保存到文本文件中时,请执行以下操作:

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. 在这里,“ celda60”是字段内单个文本字段的名称,该名称是在创建单元格时给出的。 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"; 但如果我这样做:打印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 它保存在我的文件中的唯一东西是TESTESTESTEST,所以问题就必须是param('celda60'),以防万一您怀疑'salvar()'看起来像这样

   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. 我的意思是,我猜除了很多基本的东西。

Client side modifications of an HTML document are not automatically reflected via CGI into the state of the server-side program. HTML文档的客户端修改不会通过CGI自动反映到服务器端程序的状态中。 You need an additional mechanism, eg AJAX. 您需要其他机制,例如AJAX。

You need to read a well written Perl and CGI tutorial for sometime after 1998. 您需要在1998年以后的某个时间阅读编写良好的 Perl和CGI教程。

I recommend Ovid's CGI Course . 我推荐Ovid的CGI课程 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. 您至少有一个地方可以在CGI参数中使用shell换码来运行任意命令。 That is why practices like tainting user data and 3 argument open commands are standard practices and have been for many years. 这就是为什么像污染用户数据和3个参数打开命令这样的做法是标准做法,并且已经存在很多年了。

General advice: 一般建议:

  1. Make sure you are using strict and warnings . 确保使用strict warnings
  2. Use 3 argument open with lexical handles, or just use IO::File. 用3论点open与词汇手柄,或者只是使用IO ::文件。 open my $fh, '>>', $pathtofile or die "Ouch $!";
  3. You don't have to use the CGI module with Perl to do CGI. 您不必在Perl中使用CGI模块来执行CGI。 It is one convenient way to handle parameter parsing and HTML building. 这是处理参数解析和HTML构建的一种便捷方法。 There are MANY options. 有很多选项。
  4. print can handle a list of arguments. print可以处理参数列表。 There is no need to have 50 print statements in your code. 您的代码中不需要50条打印语句。
  5. Add whitespace to your code. 在代码中添加空格。 Spacing things out makes it more readable. 放宽所有内容使其更具可读性。

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. 虽然,在我的代码中,我会制作HTML并将其传回。 I like to keep side effects, like printing stuff out, closely grouped. 我喜欢保持副作用,例如将东西打印出来。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM