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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

two submit buttons in one form 2

Status
Not open for further replies.

martinasv

Technical User
Feb 12, 2006
24
0
0
HR
Hi!

If I have two submit buttons in one form (like shown below), how do I get the "name" part in a variable to be able to use it with "if" statement?


print "<br>";
print "<form>";
print "<input type=submit name=update value=\"Write NIC name\" >";

print "<font align=middle>OR</font>";


print "<input type=submit name=delete value=\"Delete NIC name\" >";
print "</form>";
print "<hr>";
 
Hi

Not their, its. With string comparation operator.

As I remember, you use CGI.pm, and I not. So if you need practical advices too, please wait for other answers.

Feherke.
 
I know eq and ne operators are for comparing strings. But what I can't figure out is how to extract the value (or name) and put it in a variable so I can do something with it.

Something tells me I should maybe use "param()", but I don't know how.
 
Hi

Something like this. I hope there are as few errors as possible.
Code:
$r=new CGI;

if ($r->param('the_button_name') eq 'Write NIC name') {
  print "Write requested";
} elsif ($r->param('the_button_name') eq 'Delete NIC name') {
  print "Delete requested";
} else {
  print "Invalid request";
}

Feherke.
 
Hi

Or this should work with your original posted code :
Code:
$r=new CGI;
@p=$r->param;

if (grep /update/,@p) {
  print "Write requested";
} elsif (grep /delete/,@p) {
  print "Delete requested";
} else {
  print "Invalid request";
}

Feherke.
 
Feherke, for someone who doesn't use CGI, you're getting there *

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
if the buttons have unique names you don't need to check the value:

Code:
print "<input type=submit name=update value=1>";
print "<input type=submit name=delete value=1>";

if (param(update)) {
   do_something
}
elsif (param(delete)) {
   do_something_else
}
else {
   default condition
}

if you use the same name you do need to check value:

Code:
print "<input type=submit name=action value=1>";
print "<input type=submit name=action value=2>";

if (param(action)==1) {
   do_something
}
elsif (param(action)==2) {
   do_something_else
}
else {
   default condition
}

I recommend you keep you form widget names/values short and all lower or all upper case. Those long mixed-case names are too easy to mis-spell, and if you aren't using "strict" it could be very difficult to debug the script to find the mis-spelled name/value.
 
Is much more simple to give both buttons the same name, then just check its value
I beg to differ. The value of a submit button is also the text that will appear on the button. If you decide to change that text (or have multiple values for multiple langauges) you'll have to change the script too.

It's easier to follow Kevin's first example, and just check whether the uniquely named submit control sends a value, any value, to determine if it was clicked.

-- Chris

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Hi

Yes Chris, you are right. But I just prefer my way.

The name can be used for handle similar tasks at once :
Code:
<input type="submit" name="add" value="Insert before">
<input type="submit" name="add" value="Add after">
<input type="submit" name="add" value="Add all">
<input type="submit" name="delete" value="Remove">
Code:
if ($r->param('add')) {
  &add_new_item
} elsif ($r->param('delete')) {
  &delete_item
} else [gray]# ...[/gray]

If you have buttons created dynamically, maybe from a database, if you want to identify actions by button name, then you must find out a way to generate distinct & valid button names, while you already have distinct values. ( This may not be a problem in Perl, I do not know. But for example PHP will transform non-word characters into underscore ( _ ). )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top