...Retrieve data from MySQL via the DBI module -> display this data in HTML form -> make a form for someone to input a row in the MySQL DB ...

...But I don't know quite where to go with the code I have come up with which is displayed here:

I'm guessing that being told to use HTML::Template isn't exactly what you're looking for here, although it's very good advice.

If you're looking for advice on what to do to your code from here, such that you can do the DBI lookup, show the table entries and then offer an addition form then you might do the following:
tmpl file

<html> <title>test</title> <body> <TMPL_IF NAME=data> Your row was added in successfully.<p> </TMPL_IF> <h1>Data already in table</h1> <table> <TMPL_LOOP NAME=data> <tr> <td>Name:</td><td><TMPL_VAR NAME=box2></td> <td>Colour1:</td><td><TMPL_VAR NAME=box></td> </tr> </TMPL_LOOP> </table> <form> <table border=1 > <tr> <td> Color1: <TMPL_VAR NAME="BOX"> <br></td> <td> What is your name?: <TMPL_VAR NAME="BOX2"> <br> </td> <td> <input type=submit> </td> </table> </form> </body> </html>
I'm sure you can edit your cgi code appropriately to fill in that loop. Abstracting the html around the loop into your template file allows you to later change the back-grounds of your table rows or data or their fonts or many other things without having to wade through your code again. Of course if you don't like HTML source, you might not realise the benefit of this today.

Whether or not this duplicates loops depends on your code. If you do a query, then loop through all the results pushing the hashrefs you've fetched into an array and then give that to the template to loop through and display you've duplicated a loop. Perhaps you'd think it better to build your table inside your CGI code and then add that to a variable which you then display.

HTML::Template purists will hate that idea, and they should, because you'd be perverting a very powerful and beautiful tool, but if it's the best solution you have, then use it. Then again, I'd encourage you not to be afraid of having two similar loops here (using TMPL_IF to replace possible null fields with &nbsp might also help).

Jacinta


In reply to Re: HTML::Template Form Usage by jarich
in thread HTML::Template Form Usage by bladx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.