Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Passing perl variable as input name?

by RobRobson (Novice)
on Jul 22, 2016 at 10:04 UTC ( [id://1168339]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Passing perl variable as input name?
in thread Passing perl variable as input name?

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.

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

      You should see the smile on my face right now!

      Thank you very much NetWallah, it's working perfectly now, have a great day!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found