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

Okay, I'll try to simplify this as much as possible.

I have a CGI template that dynamically creates a table based on an array of userids. Next to each userid there is a checkbox. At the bottom is a submit button.

When the submit button is clicked I need the template to send a list of the checked userids back to a CGI page. My plan is to loop through the checkboxes and cat each to a hidden field using Javascript.

Problem is, I don't know how to do any of this, since I've never used Javascript and am still fairly new to CGI.

Right now my template for the form looks something like this (I've removed a lot that isn't relavent to my question):

<body visited="red" bgcolor="#ffffff" leftmargin="0" marginwidth="0" t +opmargin="0" marginheight="0" onload="currentPage();getSortStatus();" +> <table border="0" cellpadding="0" cellspacing="0" width="100%"> {?@userids --?} <tr id="row{?$_x?}" {?if $_v!=1 --?}style="display:none"{?-- $ +_v?}> <td id="username{?$_x?}" <a name="{?$USER_FIRST_NAME?} {?$ +USER_MIDDLE_INITIAL?} {?$USER_LAST_NAME?}"></a></td> <td id="check{?_x?}" <input type="checkbox" name="printcheck" +value="on"></td> </tr> {?-- @userids?} <tr> <td><input type="submit" value="Submit"></td> </tr> </table>
This displays what I want to see, but obviously doesn't do what I need it to do. I know I need a function to call onSubmit, but I don't know how to loop through a dynamically generated series of checkboxes.

So, the questions I pose to the almighty Monks are: 1. What do I need to add to the code above and 2. What will my function look like in order to pass my list of userids?

Replies are listed 'Best First'.
Re: OT? Checkbox list with CGI/Javascript
by dorward (Curate) on Nov 11, 2005 at 15:17 UTC

    For this I suggest you start by forgetting JavaScript, you don't need it. Then make use of the markup validator (you have syntax errors in your HTML). Then...

    <input type="checkbox" name="ids" value="1"> <input type="checkbox" name="ids" value="2"> <input type="checkbox" name="ids" value="3">
    and
    ... use CGI; use Data::Dumper; my $cgi = CGI->new(); my @results = $cgi->param('ids');
      This won't work, because we're generating the rows dynamically with the array @userids, and don't know how many we'll have.

        And the problem is...? Given this is a perl site, I think the exhortation to generate text given your @userids is implicit.

        <tmpl_loop userids> <input type="checkbox" name="ids" value="<tmpl_var name>"> </tmpl_loop>
        and in perl
        my %tmpl_vars = ( userids => map { {name => $_} } @userids );
        assuming you're using HTML::Template. Modify as appropriate for your own templating system. You are using a templating system, right?

Re: OT? Checkbox list with CGI/Javascript
by holli (Abbot) on Nov 11, 2005 at 15:19 UTC
    You may want to check out Ovids CGI course.

    To adress your problem: you have to parse the data from the incoming form and pass/use that to/in your template mechanism, so you can iterate over it. You don't mention what templating engine you use, so I can't be more specific here.


    holli, /regexed monk/
Re: OT? Checkbox list with CGI/Javascript
by ickyb0d (Monk) on Nov 11, 2005 at 17:05 UTC
    on that page you could have a hidden field called "all_checkboxes" or something. using various javascript and DOM stuff, you can get the values of the checkboxes (note: JS code below is just pseudo-code). on the onSubmit event, populate that field with something like...
    for(var i=0; i<total_checkboxes; i++) { if(checkbox = 'checked') { all_checkboxes.value += userid + '|||'; } }
    before the submit occours, that field should look something like "2|||3|||8|||20|||". Only filled with the checked userids.

    then in your cgi script, split that field using a split command...
    my @userids = split('|||', $cgi->param('all_checkboxes');
      Right, this is along the lines of what I was thinking.

      How does your IF statment above reference WHICH checkbox it's looking at, though. This is the part I can't figure out.

      Calculating the value for total_checkboxes is also going to be an issue.

        W3Schools is going to be your best friend, especially if you're just getting started with JS. I haven't tested this but it would most likely look something like this...

        var checked_boxes = ""; for(var i=0; i<total_checkboxes; i++) { var cur_box = document.getElementById('userbox_' + i); if(cur_box.checked) { checked_boxes += i + '|||'; } } var all_checkboxes = document.getElementById('all_checkboxes'); all_checkboxes.value = checked_boxes;

        now this is assuming all of your userid's are 0,1,2,3,... and the checkbox names are setup with the name/id as 'userbox_0', 'userbox_1', 'userbox_2',... i'm also guessing that you're user id's won't be consecutive like that. you might try populating an array (user_ids for example), then giving that to the javascript via a template variable. once that array is stored in js, you could just use user_ids.length for the total_checkboxes and just use user_ids[i] to reference the user id stored in the array.

        you might also want to look at the JSON perl module as well for transferring data to and from javascript applications. this might be confusing at the time if you're just using javascript, but you can find more information at json.org

Re: OT? Checkbox list with CGI/Javascript
by superfrink (Curate) on Nov 12, 2005 at 00:36 UTC
    Here is one method that I have used when I wanted to know which checkboxes were present on a page. (Way back when I did web work.)

    For each "thing" you want in the list produce two fields in the form, one a checkbox and one a "hidden" field. Call the checkbox fields something like "cb_N" and the hidden fields something like "hd_N" where N is a number from 1 to N and you have N "things".

    When the data is submited you will get a list of all "hd_*" fileds but the checkboxes are only submitted if they are checked off. So you loop over all of the "hd_N" values and see if the "cb_N" was submitted or not for that number "N".
      Again, the problem I see with this solution is that I won't know how many "N"s I'll have, since the list is being created dynamically depending on the data.
        You will know how many 'N's there are because there is one "hidden" variable for each 'N'. That's what the hidden variables are for. If a hidden variable was submitted then you know a checkbox exists.
Re: OT? Checkbox list with CGI/Javascript
by rashley (Scribe) on Nov 11, 2005 at 20:41 UTC
    Yes, I'm using a templating system (one I didn't write). And it's after examining that system that I realize that @userids isn't just an array, it's an array of hashes.

    I've got all the java worked out exept how to get all the "USER" values from @userids into my Javascript array.