mwerner92 has asked for the wisdom of the Perl Monks concerning the following question:

So I have the following html script, and perl cgi script for a basic calculator for Apache. What I was wondering was if it is possible to add a button to the output from the cgi script, that links back to the original html file? Basically a button to start over again after the script gives you the answer. Any tips?

HTML

<html> <head> <title>Simple Calculator</title> </head> <body> <h1> Simple Calculator </h1> <form action=/cgi-bin/calc.pl method=post> <center> First Number: <input type=text name=na maxlength=4 size=4><p> Second Number (or Exponent Number): <input type=text name=nb maxlength +=4 size=4><p> Preferred mathematical operation:<br> <input type="radio" name=oper value="mul">Multiply<br> <input type="radio" name=oper value="div">Divide<br> <input type="radio" name=oper value="add">Addition<br> <input type="radio" name=oper value="sub">Subtraction<br> <input type="radio" name=oper value="expo">Exponent<br> <p> <input type="submit" value="Submit form"> <input type="reset" value-"Clear all fields"> </form> </body> </html>

Perl CGI:

#!/usr/bin/perl -w use CGI ':standard'; $na = param('na'); $nb = param('nb'); $oper = param('oper'); if ($oper eq "mul") { $answ = $na * $nb; } elsif($oper eq "div") { $answ = $na / $nb; } elsif($oper eq "add") { $answ = $na + $nb; } elsif($oper eq "sub") { $answ = $na - $nb; $nd = "-"; } else {$answ = $na ** $nb; } print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title> Answer Page</title>"; print "</head>"; print "<body>"; print "<H2> The answer to your calculation is: $answ </H2>"; + print "</body>"; print "</html>";

Replies are listed 'Best First'.
Re: Not sure if possible?
by NetWallah (Canon) on Jul 08, 2016 at 00:36 UTC
    $ENV{HTTP_REFERER}
    That variable tells you from whence you came to the current (CGI) page.

    If you create an anchor (a) link back to that, Bob could very well be your uncle!.

    See also, This ancient discussion.

            There is no time like the present for postponing what you ought to be doing.

Re: Not sure if possible?
by $h4X4_&#124;=73}{ (Monk) on Jul 08, 2016 at 09:05 UTC

    I was going to mention CGI Redirect, but thats way more than what you need.
    For this script just add a simple link back to the starting script.

    <a href="Calculator.html" target="_self">Back to Calculator</a>
    If you want it to look like a button there are a few CSS and other HTML tags that can be used to give the effect and its easy to add a CSS class to the link above.
    Update: to add the link in the quotes you have in print you will need to escape the links quotes \"\".
    print "<a href=\"Calculator.html\" target=\"_self\">Back to Calculator +</a>";
    Or I would just wright it like this.
    print <<"HTML"; Content-type:text/html <html> <head> <title> Answer Page</title> </head> <body> <p> <h2> The answer to your calculation is: $answ </h2><br /> <a href="Calculator.html" target="_self">Back to Calculator</a> </p> </body> </html> HTML

Re: Not sure if possible?
by Anonymous Monk on Jul 07, 2016 at 23:27 UTC
    sure, its is possible, buttons can do all sorts of thing