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

buttons decide which function to run?? 5

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
BE
hi,

I want to be able to let the user decide what to do by clicking on a button (like in a form)... if the user clicks on the first button then "sub click1" should be run and if the user choses button2 then "sub click2" should be run... BUT, I don't know how to get the info out of the form so that I know which button has been pushed... I can call the fuction (&the_function_name) but I don't know how to check which button the user has pushed...

(Also for security reasons, the form (or whatever) should be printed/made in de perl script itself)

Can anyone help me??

Thanks,
Math
 
Have hidden fields within each form. I would create a different form for each button.

For example:

<form action=&quot;file.cgi&quot;>
<input type=hidden name=button value=button1>
<input type=submit value=&quot;Button One&quot;>
</form>

<form action=&quot;file.cgi&quot;>
<input type=hidden name=button value=button2>
<input type=submit value=&quot;Button Two&quot;>
</form>

Then in your perl script:

[tt]

if ($FORM{action} eq &quot;button1&quot;) {
&sub1;
} else {
&sub2;
}

[/tt]

Hope this helps.

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
I have not run this, but, I think it is close to correct. It illustrates using CGI.pm and image buttons. Clicking on one of the buttons populates the (x,y) coords of the click within the image. Then, just check to see if an 'x' value exists. If so, that button was clicked.

Code:
#!/usr/local/bin/perl
use CGI;
$query = new CGI;
print $query->header;
print $query->start_html,$query->center;
if ($query->param('button1.x'))    { &firstSub; }
elsif ($query->param('button2.x')) { &secondSub;}
else  { &frontPage; }
print $query->end_html;

sub frontPage {            
my @buttons = ('button1','button1');
my %buttons = ('button1','one.gif','button2','two.gif');    
my ($button,@buttons,$srcURL);
print $query->start_form(-action=>&quot;$thisCGI&quot;,
                         -method=>&quot;POST&quot;);
foreach my $button (@buttons)
    {
    my $pic = $buttons{$button};
    print &quot;\n&quot;,$query->image_button(-name=>&quot;$button&quot;,
                            -src=>&quot;$pic&quot;,
                            -align=>'MIDDLE',
                            -border=>'0');
    }
print '</center>',$query->endform;
}
sub firstSub {}
sub secondSub {}


Obviously, firstSub and secondSub would need to be improved a little.

'just another way to do it....
HTH


keep the rudder amid ship and beware the odd typo
 
There are at least two easy methods that we use here. The first is form based using radio buttons (little circles you can click on). The NAME parameter is the same but the assigned VALUE will be different. Then, in the cgi you would compare the NAMED FIELD to the corresponding value. An example is:

<TABLE CELLSPACING=0 CELLPADDING=3 BORDER=0>
<TR><TD BGCOLOR=\&quot;#0000A0\&quot; ALIGN=CENTER>
<FONT SIZE=3 COLOR=\&quot;#FFFFFF\&quot; FACE=\&quot;arial,helvetica\&quot;>Main Menu
</FONT></TD></TR> <TR><TD BGCOLOR=\&quot;#0000A0\&quot; ALIGN=CENTER>
<FONT SIZE=2 COLOR=\&quot;#FFFFFF\&quot; FACE=\&quot;arial,helvetica\&quot;>Select From the Following Options</FONT></TD></TR>
<TR>
<TD WIDTH=300 BGCOLOR=\&quot;#FFFFD8\&quot;>
<FONT SIZE=2 COLOR=\&quot;#000000\&quot; FACE=\&quot;arial,helvetica\&quot;>
<INPUT TYPE=RADIO NAME=OPT VALUE=1>&nbsp;View Account Information</FONT></TD>
</TR>

With this you would compare the content of field OPT with either a 1, 2 or 3 to determine what to do.

This next example uses individual image files for buttons. To display you would assign the NAME parameter differently. However, when clicked, the browse returns a non-blank value in the field for the botton clicked. Just check the field(s) for value and you will know what to do.

Here is an example of outputting the buttons this way:

<!-- Function Button Area -->
<INPUT TYPE=IMAGE NAME=CUACT1 HEIGHT=\&quot;20\&quot; ALT=\&quot;Logon\&quot;
WIDTH=\&quot;75\&quot; SRC=\&quot;$cx_main::CUSERVER/images/logon.gif\&quot; BORDER=\&quot;0\&quot;>
<INPUT TYPE=IMAGE NAME=CUACT2 HEIGHT=\&quot;20\&quot; ALT=\&quot;Home\&quot;
WIDTH=\&quot;75\&quot; SRC=\&quot;$cx_main::CUSERVER/images/home.gif\&quot; BORDER=\&quot;0\&quot;>

The, to check which one was clicked, extract the values from the form as in:

my $CUACT1=$form{'CUACT1.x'};
my $CUACT2=$form{'CUACT2.x'};
my $CUACT3=$form{'CUACT3.x'};
my $CUACT4=$form{'CUACT4.x'};
my $CUACT5=$form{'CUACT5.x'};

Then just check the field(s) for a value as in:

if ($CUACT1 gt &quot;&quot;) { ....

Hope this helps somewhat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top