1
votes

I had an HTML form that sent checkbox values to a Perl CGI script as an Array. However, since the site was rebuilt using mainly PHP, the checkbox array is handled differently. I have a PHP function that returns the form. It looks something like this:

<td>Profiles: </td>
<td><input type=\"checkbox\" value=\"oneconnect\" name=\"v1-profile[]\">OneConnect <br />
<input type=\"checkbox\" value=\"http\" name=\"v1-profile[]\">HTTP <br />
<input type=\"checkbox\" value=\"xforwardedfor\" name=\"v1-profile[]\">Xforwarded-for</td>
</tr>

I then send this to a Perl CGI script

use CGI qw(:standard);
my $q = new CGI;
my @profiles1 = $q->param("v1-profile");

When I try to print the elements of the array, I only see the word "Array" as the output.

foreach my $r (@profiles1) {
print "$r\n";
}

I have also tried a few things that did not work.

foreach my $r (@profiles1) {
foreach my $v (@$r) {
print "$v\n";
}
}

How would I access the elements of the "@profiles1" array? Thank you for the help!

2
Are you seeing something like ARRAY(0x27967ac)? This is different from Array and means you have an array reference that simply needs dereferencing.Borodin

2 Answers

2
votes

The trailing [] on the variable name is a PHP-ism -- it's not standard, and isn't treated specially by Perl's CGI module (or anything other than PHP, really). If you can, remove it from the form. If not, you should be able to get at the parameter from Perl by including the brackets in the name:

my @profiles = $q->param("v1-profile[]");
1
votes

Not sure what your problem is. It seems to work fine for me. Here's the little test rig I built.

test.html:

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1>Test</h1>
    <form action="/cgi-bin/form">
      <table>
        <tr>
          <td>Profiles: </td>
          <td><input type="checkbox" value="oneconnect" name="v1-profile[]">OneConnect <br />
          <input type="checkbox" value="http" name="v1-profile[]">HTTP <br />
          <input type="checkbox" value="xforwardedfor" name="v1-profile[]">Xforwarded-for<br />
          <input type="submit"></td>
        </tr>
      </table>
    </form>
  </body>
</html>

cgi-bin/form:

#!/usr/bin/perl

use strict;
use warnings;
use CGI;

my $q = CGI->new;

print $q->header(-type => 'text/plain');

my @profiles = $q->param('v1-profile[]');

foreach (@profiles) {
  print "$_\n";
}

And I see exactly what I expect. Each checkbox I check is displayed in the output.

One thing to check. What does your URL look like once you have submitted the form? Mine looks like this (with two checkboxes checked).

http://localhost/cgi-bin/form?v1-profile%5B%5D=oneconnect&v1-profile%5B%5D=xforwardedfor

Notice that the square brackets in the input names have been URL-encoded. That's what should happen.

So the question is, how does this differ from your set-up?