in reply to Passing perl variable as input name?

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);

Replies are listed 'Best First'.
Re^2: Passing perl variable as input name?
by RobRobson (Novice) on Jul 21, 2016 at 15:50 UTC
    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

        Hi NetWallah,

        Really appreciate this! I have adjusted the input as suggested but still no luck.

        I am currently pulling the parameter like this as previously suggested:

        my $selectedName = param($unit_name);

        Here is the current state of my while loop which is able to dynamically create buttons on the page but when clicking on them the page simply refreshes and no parameters are sent through:

        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='hidden' name=unit_name value='$unit_name'> +"; print "<input type='image' name='$unit_name' id='$unit_name' a +lt=airConButton src='../../images/unit.jpg' height='100px' width='100 +px' onclick='document.forms[0].unit_name.value =\"$unit_name\";this.f +orm.submit()'></input>"; }

        Here are my current URL paramters:

        ViewUnits.pl?unit_name=Air+Con+1&Air+Con+1.x=68&Air+Con+1.y=24&unit_name=Air+Con+2&unit_name=Air+Con+3&unit_name=Air+Con+4&unit_name=Air+Con+5&unit_name=Air+Con+6&unit_name=Air+Con+7&unit_name=Air+Con+8&unit_name=Air+Con+9

        When putting the hidden input field after the while loop, the URL parameters will always equal "Air Con 9" yet the value still does not display when attempting to print:

            print "<br>Name: $selectedName<br>";

        If you need anything else from me or have any other suggestions please let me know. I will continue to research and update if I am able to find a solution!

        UPDATE: After playing around with the cgi I have found when setting: $selectedName = param('unit_name'); This links to the hidden input field, button clicks now make the selectedName variable equal "Air Con 1" regardless of which button is pressed. Still not creating the names as expected but at least I am passing a value now.