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

I'm working on a very specific problem at my work place and have not been able to find answers for what I'm searching for I'm looking to retrieve information from a MySQL DB and adding it to a form so it can be added(submitted) to a different DB, but before submitting the user needs to be able to make changes and add more data to the retrieved information form the first database.(imagine "copy and pasting if you will" from first database into the HTML form without submitting, if it makes any sense) I've been stuck on this problem for for a few days. Any help or ideas are appreciated.
  • Comment on Perl: info from database into a form(HTML)

Replies are listed 'Best First'.
Re: Perl: info from database into a form(HTML)
by Corion (Patriarch) on Jul 29, 2016 at 20:42 UTC

    So, what code have you written and where exactly do you encounter problems?

    Most of what you want should be easily achievable by using DBI and CGI, but unless you tell us where exactly you encounter problems, we can't help you much.

      Thank you for trying to help me.

      I have the HTML button and the SQL statmenet ready but dont know where to go from here?

      **HTML:** print " <div style=\"z-index:86;\" class=\"group-shell\">"; print " <table>\n"; print " <tr><td><input class=\"dial-red-button\" id=\"importFrom +CAD\" type=\"submit\" value=\"Import Info\"></td></tr>\n";

      **javascript-jquery**

      <script type="text/javascript">

      \$(document).ready(function() {

      \$('#importFromCAD').click(function () {

      \$('#importNav').val('');

      return true;

      **Perl SQL statement:**

      $sql_statement20 = "select * FROM CT_AL_CAD_ID where CAD='$CAD' ORDER BY KeyField DESC";

      $sth20 = $dbh->prepare($sql_statement20);

      $sth20->execute();

      $sth20->bind_columns(undef, \$ID_data[0], \$ID_data1, \$ID_data2, \$ID_data3, \$ID_data4, \$ID_data5, \$ID_data6, \$ID_data7, \$ID_data8, \$ID_data9, \$ID_data10, \$ID_data11, \$ID_data12);

      $sth20->fetch();

        This is really where I'm stuck at and have no clue how to proceed, how do I display the retrieved info from the database to the user in the form fields so it can be modified without the info being submitted to the second database first.
Re: Perl: info from database into a form(HTML)
by haukex (Archbishop) on Jul 30, 2016 at 14:59 UTC

    Hi alen129,

    By "different DB", do you mean a second database on the same server, or does it need to be submitted to a different server?

    There are several ways to approach this problem. One "classic" way would be to have your CGI script read from the database via DBI (you've already got code for that), generate HTML (looks like you've already got some of that, I'll make a suggestion below), and when the HTML form is submitted back to the same or a different CGI script, that script can write it to the second database.

    Another possible approach, which I'm suggesting because you're already using jQuery, is to have most of the code written in JavaScript as part of the HTML page, and then have the Perl CGI script not generate any HTML, but only access the databases (again via DBI), and it could speak to the script in the browser with JSON (see e.g. jQuery.ajax()).

    The latter approach is the way many modern websites work, but it requires a good understanding of JavaScript. The first approach is the "classic" way of doing web forms, which doesn't necessarily require any JavaScript and therefore works in just about any browser.

    To get back to your question, it sounds to me like what you're missing here is how to get the data from the database into the HTML? Here's one way:

    use warnings; use strict; use CGI qw/ escapeHTML /; # this is the data you'd normally get from the DB my @cols = ( "foo", "bar", "q\"><uz" ); print qq{<table>\n<tr>\n}; for my $col (@cols) { print qq{\t<td><input type="text" value="}, escapeHTML($col), qq{"></td>\n}; }; print qq{</tr>\n</table>\n}; __END__ Output: <table> <tr> <td><input type="text" value="foo"></td> <td><input type="text" value="bar"></td> <td><input type="text" value="q&quot;&gt;&lt;uz"></td> </tr> </table>

    However, there are also much better ways to generate HTML than a bunch of prints, just one example of many is Template::Toolkit, or even modern web frameworks like Dancer, Catalyst, or Mojolicious. For simple stuff I like the latter, there are some tutorials here.

    Hope this helps,
    -- Hauke D