in reply to Re: Passing perl variable as input name?
in thread Passing perl variable as input 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?

Replies are listed 'Best First'.
Re^3: Passing perl variable as input name?
by NetWallah (Canon) on Jul 21, 2016 at 16:21 UTC
    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.

        THere are several issues you need to correct.

        First - you need to fix how you fech the param value - at the time the form' s values are being collected, $unit_name does not exist - so you need to do:

        my $selectedName = param('unit_name');
        Next, you need to create a single hidden field OUTSIDE the loop:
        print "<input type='hidden' name='unit_name' value='This gets set via + javascript'>";
        Next, your loop should look something like this:
        print "<td>\n"; # If you want all buttons in a single TD .. otherw +ise, this could be <tr> while (my @data = $sth->fetchrow_array()) { $unit_name = $data[0]; push(@unitNames, $unit_name); print "$unit_name<br>"; print "<input type='image' name='$unit_name' id='$unit_name' a +lt=airConButton src='../../images/unit.jpg' height='100px' width='100px' onclick='this.form.unit_name.va +lue =\"$unit_name\";this.form.submit()'></input>"; } # Close the TD or TR here..

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