Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

CGI Table with checkboxes

by halecommarachel (Sexton)
on Apr 12, 2013 at 17:06 UTC ( #1028424=perlquestion: print w/replies, xml ) Need Help??
halecommarachel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I need some help creating a table with checkboxes in the first column. Once the user selects as many checkboxes as he/she needs, I want to generate a textbox at the top of the page with a command in plain text format, generated from the data in each checkbox's corresponding table row. Some additional functionality I need is the option to clear all/select all. So far in my cgi script, I've collected the data needed to populate the tables and created the header row.

Replies are listed 'Best First'.
Re: CGI Table with checkboxes
by davido (Archbishop) on Apr 12, 2013 at 17:42 UTC

    If you're using, you just "print" the header, followed by the HTML as it should be passed along to the browser. So if you can construct the table with plain-old-HTML, just do the same using print statements from Perl (don't worry about the helper tags if they just complicate things).

    But as for how to program the entire interactive process, that's not only a little more than I'd be prepared to write in a reply here, it's been covered at length. Look at Ovid's CGI course, for example, or CGI Programming with Perl (O'Reilly -- The mouse book, 2nd edition). Both are really old (Ovid's course is on the way-back machine), but that's because CGI itself is really old.

    If you're just starting out this whole process, I would personally recommend not going the bare-CGI route, and instead, use a framework like Mojolicious. Create a Lite App. It's a lot simpler, in my opinion, than dealing with plain old CGI, and it can still work in a CGI environment. Eventually you'll find that you still need to learn the CGI fundamentals, but Mojolicious and other modern frameworks make it so much nicer. In the case of a "lite app", you simply set up hybrid controller/routes that populate templates. It's pretty slick.


Re: CGI Table with checkboxes
by ww (Archbishop) on Apr 12, 2013 at 17:16 UTC
    Do you know how to do it with HTML? Have you read the module's doc ( perldoc CGI::Table )? Have you made any attempt to find the answer anywhere?


    Then please do so, and come back with a well written question ( How do I post a question effectively? ), your code, and a clear description of how you're struck when you can answer all the questions above with a "yes."

    If you didn't program your executable by toggling in binary, it wasn't really programming!

      I have already written the table in html, read the module's docs, and made an attempt to find the answer elsewhere. Thanks for checking. Is it possible to give an answer without any explicit perl code? I don't know how to do this with html and a cgi script, or a cgi script alone.

        It would make life simpler if you showed us a code sample of what you have tried. I can give you an idea of how I have done something almost exactly like this in the past but it may sound vague and may not fit your needs.

        I've done this completely using the CGI module and HTML::Table. The key thing to remember is that your form should completely enclose the table. Each row of the table may have one or more hidden fields (possibly to identify the row number). The option to select all/deselect all was done in javascript, if I remember right.

        See this is vague. If you can show code with minimal example of what you are trying to do, we will certainly help you figure out a great way to tackle your problem.

Re: CGI Table with checkboxes
by TJPride (Pilgrim) on Apr 13, 2013 at 06:29 UTC
    Why do this with Perl at all? Why can't you just use Javascript to display / fill in the values? Something like:
    <html> <head> <title>Checkboxes to Textarea</title> <script type="text/javascript"> function refactor() { var f = document.forms.myform; var s = []; for (var i = 1; i <= 3; i++) if (f.elements['C'+i].checked) s.push(f.elements['C'+i].value); f.command.value = s.join(' '); document.getElementById('command_div').style.display = s.length > 0 ? '' : 'none'; } </script> </head> <body bgcolor="white"> <form name="myform" method="post" action=""> <div id="command_div" style="margin-bottom: 15px; display: none;"> <textarea name="command" rows="3" cols="50"></textarea> </div> <input type="checkbox" name="C1" value="Command 1" onclick="refactor() +;"> Checked Command 1 <input type="checkbox" name="C2" value="Command 2" onclick="refactor() +;"> Checked Command 2 <input type="checkbox" name="C3" value="Command 3" onclick="refactor() +;"> Checked Command 3 </form> </body> </html>
    I'm assuming of course that the command is for display purposes only, and will not be submitted directly to the system. You have to be very careful with that sort of thing even if you're filtering it through Perl first.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028424]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2018-04-19 21:31 GMT
Find Nodes?
    Voting Booth?