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

Hi Guys, I have been searching google for hours and can't find a solution to this problem so any help will be fantastic.

I'm creating image buttons via a while loop as I need these buttons to be created dynamically because it pulls data from a mysql database. I need these buttons to work independently. So for example, pressing 'button1' will make 'action1' happen.

I have currently set the input name of the image buttons to the names I am pulling from the database using the Perl variable "$unit_name". Inspecting the buttons on the website itself I can see the values are being set to the names I am expecting, and when setting the form method to GET I can see the names in the URL bar.

When you press a button I intend for the form to submit so the name of whichever button has been pressed can be sent through as a parameter. However at the moment the variable "selectedName" receives no data.

Here is where I pull the parameter:
my $selectedName = param('$unit_name');
Here is the while loop I am using to create the buttons:
my $statement2 = "select unit_name from unitNamesTable"; my $sth = $dbh->prepare($statement2); $sth->execute(); while (my @data = $sth->fetchrow_array()) { $unit_name = $data[0]; push(@unitNames, $unit_name); print "<td>$unit_name<br>"; print "<input type=image name='$unit_name' id=$unit_name value='$u +nit_name' alt=airConButton src=../../images/unit.jpg height=100px wid +th=100px onclick=this.form.submit()>"; }
If you need anything else or have any ideas how I can either fix this or go about it in a better way please let me know. Thanks in advance!

Replies are listed 'Best First'.
Re: Passing perl variable as input name?
by Corion (Patriarch) on Jul 21, 2016 at 15:30 UTC
    my $selectedName = param('$unit_name');

    $selectedName will always be empty unless the CGI/HTTP request literally passes a parameter name of $unit_name including the dollar sign.

    You don't need the single quotes if you want to simply pass a variable:

    my $selectedName = param($unit_name);
      Hi Corion,

      Thanks for replying. I've made the change to the parameter name, however I'm running into the same issue where "$selectedName" displays nothing after a button press. After checking the URL this is what is being passed:

       ViewUnits.pl?Air+Con+1.x=54&Air+Con+1.y=55

      At this point I just need the value "Air Con 1" to be put in "$selectedName".

      Do you know if it will be possible to do this?

        You are creating a NEW button ID each time through the loop.

        Your print should be something like:

        print "<input type=image name='$unit_name' id='unit_name' value='$unit +_name' alt=airConButton src=../../images/unit.jpg height=100px width= +100px onclick=this.form.submit()>";
        Now, the "submitted" page will receive
        param('unit-name')
        although, if you have multiple buttons with the same id (unit_name), you may get an array of values.

        UPDATE:You should get the value of the button used to submit (Untested).

        UPDATE 2Revising unsatisfactory answer.
        The trick is to submit a different value for the same Name, via Javascript.
        Untested: (You need to CREATE a Hidden form field with the name "unit_name")

        print "<input type='image' name='$unit_name' alt=airConButton src='.. +/../images/unit.jpg' height='100px' width='100px' onclick='document.forms[0].unit_name.value =\"$unit_name\";this.form.s +ubmit()'>";

                "Software interprets lawyers as damage, and routes around them" - Larry Wall