Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form and Input question

Status
Not open for further replies.

zsfhaergta

Programmer
Oct 6, 2008
26
US
Hi,

I would like to have a form with a method get perform an action, using the value from the input line. But I want to display a constant "click" in the submit button box. The value passed as the parameter should be "$ref" while the display should be "click". How can I do this? Currently I have:

<form method="get" action="url">
<input type="submit" value="$ref">
</form>

so the value is correct($ref), but I want the button to display "click
 
You can't do that with a submit button. Once you click the Submit button, it will change the text on the button for a brief second, then submit the form. This will cause the page to reload and you will lose the "Click" on the button.

If you really need the ability, I would change the Submit to a button and onclick submit the information via AJAX.
 
You could use Javascript to change the value of the submit button when its clicked, but before it submits the form.

Though you'll need to keep the value somewhere JS can access it.

Code:
<input type=hidden id="refvalue" value="$ref">
<input type=submit name="button" value="Click" OnClick="this.value=document.getElementById('refvalue').value">


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks, that seems to be exactly what I'm looking for. however, I am writing this code using perl, is there a similar solution for that?

Thanks
 
I would not think its possible to do from Perl, but you can ask in the forum219 to be sure.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thats getting me a lot closer, the problem is that I have multiple input statements:

<form...>
while (my $ref = $sth->fetch()) {
print "<input type=\"submit\" value=\"Click\">";
print "<input type=\"hidden\" value=\"$ref\">";
};
</form>

However, the hidden seems to grab the first value in the list, how can I have the value of $ref correspond to the value of the button clicked in submit?
 
Two possible ways:

1) Start a new form for every iteration of the loop:
Code:
while (my $ref = $sth->fetch()) {
print "<form...>"
print "<input type=\"submit\" value=\"Click\">";
print "<input name="val" type=\"hidden\" value=\"$ref\">";
print "</form>"
}
2) Depending on the range of values of $ref, you could be a bit sneaky:
Code:
<form...>
while (my $ref = $sth->fetch()) {
print "<input name="press-$ref" type=\"submit\" value=\"Click\">";
}
</form>
When the form is submitted, just loop though all the parameters to find the one starting "press-":
Code:
$q = new CGI;

foreach $key ($q->param) {
   if ($key =~ /^press-/) {
      $ref_pressed = $';
      last;
   }
}
You may need to check for characters that aren't valid in a name first though, like spaces for example.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
I think easiest would be to give each hidden input a unique ID, and use that in each button's Onclick event.

Code:
[red]$i=0;[/red]
while (my $ref = $sth->fetch()) {
print "<input type=\"submit\" value=\"Click\" [red]onClick=\"this.value=document.getElementById('refvalue$i').value\"[/red]>";
print "<input type=\"hidden\" value=\"$ref\" [red]id=\"refvalue$i\"[/red] >";

[red]$i++;[/red]
};


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks a lot guys, I went with ChrisHunt's first solution. It seemed the most straight forward way to approach the problem. I tried your solution vacunita, but it didn't seem to work, it was still only picking up the first element of the list..maybe because onClick is a javascript function?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top