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

Hi, I am trying to pass data from one CGI script to another using a hyperlink. I know that I can use a link such as this:
<a href="cgi2.cgi?variable1=value1&variable2=value2>
This method (I believe) forces me to use the GET method and displays all of the variables in the hyperlink. I would like to pass the variables using a POST statement if that is possible. I thought that I could so something like this:
<form action="cgi2.cgi" method=POST> <input type=hidden name=variable2 value=value1> <a href="cgi2.cgi">next page</a> </form>
This doesn't seem to work though and it looks suspect since I am placing "cgi2.cgi" in two seperate places. Does anyone know if what I am trying to do is possible? There must be another way to pass variables aside from using the rather ugly, clunky button input items. Thank you for taking the time to read this. I would very much appreciate any help. Joe

Replies are listed 'Best First'.
Re: POST from a hyperlink
by dragonchild (Archbishop) on Apr 25, 2004 at 02:28 UTC
    The answer involves using JavaScript.
    <form action="cgi2.cgi" method="POST" name="thisForm"> <input type="hidden" name="variable2" value="value1" /> <a href=" " onClick="thisForm.submit()">next page</a> </form>
    I'm not fully positive on the exact parameters to the <a> tag. I've only tested this with IE 6. It might not work correctly on other browsers.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      Thanks for that example. I'll give it a try and throw it through a few different browsers. I appreciate your help. Joe
Re: POST from a hyperlink
by Anonymous Monk on Apr 25, 2004 at 02:30 UTC

    You either have to use the submit button, or you have to use javascript. You can use something like the following, but I advise against it. It will break for anyone without javascript... and that can be a lot of people. Best to stick with normal links with GET content... that's what it's there for!

    <form action="cgi2.cgi" name="frmGo" method="post"> <input type="hidden" name="variable2" value="value1" /> <a href="javascript:document.frmGo.submit()"> next page </a> </form>
      Thanks for you advice. I am also leary about using javascript but I'll reconsider what type of data I send and perhaps use the GET statement as you suggest. Thanks again. Joe
        Note that data sent via POST is no more secure than data sent via GET. It's still data that's visible from the client-side. If you have data you need to keep secure, the best way is to not send it to the client at all. You will need sessions and a lot more infrastructure that's beyond the scope of my Sunday morning reply. :-)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: POST from a hyperlink
by TwistedGreen (Sexton) on Apr 25, 2004 at 18:22 UTC
    You should know that you can also use CSS to change your submit button to look like whatever you want: change its colours or borders, or replace it with another image entirely.
      <input type="image" src="/whatever.gif"> will also work and doesn't require CSS (not that CSS is a bad thing, just that an image input is simpler).
        It also won't submit the form as written. Using anything other than an input of type "submit" will not submit a form without some form of client-side scripting, usually JavaScript.

        As for CSS, using CSS provides a lot more, and should be recommended in general.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

          A reply falls below the community's threshold of quality. You may see it by logging in.

        It might be simpler, but maybe I'm using a terrible old and slow modem and on top of that, it takes too many hops to reach your server. In that case, I can switch off CSS and get the default submit button, instead of downloading your whatever.gif, what should be a whatever.jpeg or whatever.png to begin with ;-)

        And another nice advantage of the CSS approach is that maybe in a month time you like to restyle your website and you have a new whatever image. The bad thing is that you were cheap and chose a webhosting company that only allows you to use FTP (yeah, they are out there ;-) Now you need to download all the files that contain the reference to this image, change them and upload them again. With the CSS approach, you simple alter the CSS file and upload that one.

        --
        b10m

        All code is usually tested, but rarely trusted.
        That's a good shortcut. Seems easy to implement. Thanks for the tip. Joe
      Neat! You learn someting new every day. That would certainly solve my form uglieness problem. Thanks for the info. Joe