Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
...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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found