josef has asked for the wisdom of the Perl Monks concerning the following question:

Hallo Perlmonks,

I have a form with a table where new rows can be added/deleted dynamically by clicking on add/remove button. The new rows are now added at the end of the table, but in the future I must have the possibility to insert new rows in any position in the table and move up/down the rows. The table will contain Access List and the order of rules is critical.

The new rows contain more columns with input fields, checkboxes, radio buttons and a select list. The table is populated with data from a small database (and all form elements used now unique name). I used Perl and JavaScript to do this job.

My problem begin when I try to save the new added/removed rows back into the database. When I submit the form, the POST string not contain my new added data. Yes, I know what is happening but I have no solution.

My question is, how can I save my changes (with new added rows) from the HTML table into the database? I wish to use simple programming style (KISS style) and not big/complicate module. Template style and performance is not important for me, because will be one page and only one user will work with the application. The security is very important.

I wish to use only perl (and perhaps JavaScript, but no AJAX) for my work. If it's necessary, I can restart all over again in accord with your guidance.

Thanks in advance for your sugestions

Josef
  • Comment on Save data from a dynamic HTML table into database

Replies are listed 'Best First'.
Re: Save data from a dynamic HTML table into database
by Corion (Patriarch) on Jun 06, 2012 at 17:10 UTC

    If you want to save the data, you will need to send a (POST) request with the data, and the actions (replace, or add).

    If you already send some POST request but it is missing your new rows, you will need to find out how to add these new rows to your POST request. Finding that out is a matter of investigating your Javascript library and has nothing to do with Perl.

Re: Save data from a dynamic HTML table into database
by aaron_baugher (Curate) on Jun 06, 2012 at 18:24 UTC

    This is a bit of a guess, but without some sample data and code, that's the most you can hope for: When you dynamically add elements to a page with Javascript, they may not automatically be recognized by your browser as part of the form in question. I know that, in jQuery, elements added after the initial page load are not bound to events by default. For instance, a click function that you bind to all buttons will not be bound to new buttons that are added later, unless you redo the binding or use some other special method like delegation. I don't know if the same sort of thing happens with plain Javascript and form fields, but it could be a place to look. If you're certain that the dynamic fields are not appearing in the POST data, then the Perl script on the server that processes the data isn't your problem; you'll have to look at how the fields are being added.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: Save data from a dynamic HTML table into database
by muba (Priest) on Jun 06, 2012 at 21:23 UTC

    You illustrated your problem rather well. I have a clear idea of the setup we're talking about here, the goal you have in mind, and the way it isn't accomplishing this goal. So that really is a great start.

    I also think it's an interesting problem. We know the sort of thing you're trying to achieve is possible, because you see that sort of thing all over the web. I feel like I want to give a try at tackling the problem. I'd love to see what you got so far - how do you set up the form initially, how are you currently adding rows - that sort of thing, because I'd like to work with that instead of figuring that all out myself.

Re: Save data from a dynamic HTML table into database
by thomas895 (Deacon) on Jun 07, 2012 at 05:11 UTC

    While I don't usually upvote questions, yours was particularly interesting and well-formed.
    I don't know what you're using in terms of ordering the JavaScript list, but I have seen Docking Boxes work quite well.

    In terms of the Perl part, I could probably write something, but it would be rather redundant and probably not what you are looking for. Like aaron_baugher said, we could help you more if you posted your code, or at least important parts of it.

    ~Thomas~
    confess( "I offer no guarantees on my code." );
Re: Save data from a dynamic HTML table into database
by flexvault (Monsignor) on Jun 07, 2012 at 14:20 UTC

    josef,

      ...performance is not important for me, because will be one page and only one user will work with the application. The security is very important.

    Just generate the form in pure Perl!

    Each time your click to 'add/edit/delete' the entire form data will be sent to your cgi script. Use Perl to update and send the updated form back to the browser. When the final form is complete have a button to 'SAVE' the contents of the form, and at that time also save the updated information to your database.

    This is personal preference, but use a hash to save the data, so that fields of the form are like 'field1', 'field2', etc. This allows you to reformat the fields each time the form is sent. You need the number (1..n) to maintain the order of the data.

    use strict; my $fields = ""; # input from web form my $sep = chr(254); # something unique could be '%%%' need to up +date form my $text = ""; # html document ( could be read from file ) # on return just send to browser my %TWORK = (); # Processed fields from form Template(\$text,\%TWORK,$sep); print qq|Content-type: text/html\r\n\r\n $text \r\n\r\n|; sub Template { my $text = shift; my $hash = shift; my $sep = shift; $$text =~ s{ $sep ( .*? ) $sep } { exists( $hash->{$1} ) ? $hash->{$1} : "" }gsex; }

    I can't take credit for the 'sub', I think I found it on PM, but what a great tool to add to your saved scripts. I use this tool whenever I don't know how big the form will be, and it just keeps on working. You need to add more code to get this working, but hopefully you'll have a good start now.

    Good Luck

    "Well done is better than well said." - Benjamin Franklin