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

is it possible I can pass value of array into checkboxe field of form, and to create dynamic of checkboxes also display line by line? how?

Replies are listed 'Best First'.
(jeffa) Re: Dynamic CheckBoxes
by jeffa (Bishop) on Oct 10, 2001 at 01:53 UTC
    Vague question . . . but i will attempt it anyways.

    Below is a script that uses CGI.pm to generate HTML code. If the query var 'checks' is NOT present, a form of 5 text boxes (each named 'checks') is printed out with submit button. When the user fills out ALL boxes (i did not include any validation code), the form is submitted and those names are used to dynamically create a checkbox group. The -linebreak option in CGI's checkbox_group() method prints the checkboxes vertically, instead of horizontally.

    use strict; use CGI qw(:standard); print header, start_html, start_form; if (my @checks = param('checks')) { print checkbox_group( -name => 'myName', -values => \@checks, -linebreak => 1 ); } else { print textfield('checks'), "<br>" for (0..4); print submit; } print end_form, end_html;

    Hope this helps,
    jeffa

Re: Dynamic CheckBoxes
by jepri (Parson) on Oct 10, 2001 at 02:27 UTC
    If you use HTML::Template then you can pass it an array of hashes and have it iterate over the array, printing out a checkbox for every element in the array, which is what I think you want.

    The HTML:Template code to do this looks like this(cut straight from some production code):

    <TMPL_LOOP NAME=fields> <INPUT TYPE=CHECKBOX NAME="field_<TMPL_VAR NAME="id">" <TMPL_VAR NAME= +"value">> <TMPL_VAR NAME="text"> <br> </TMPL_LOOP>

    You must then pass an array of anonymous hashes which look like this: { id => "id number", value => "checkbox value", text => "What the user sees next to the checkbox" }. In your program this would look like(untested code):

    push @array, { id => "id number", value => "checkbox value", text => " +What the user sees" };

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.