home support FAQ resources services partners contact us contact us
 MySQL Tutorial Previous  Next  
 

A script can allow itself to be invoked by sending to the browser a page containing a self-referential hyperlink—that is, a link to its own URL. For example, if a script myscript.pl is installed in the Web server's cgi-bin directory, it can produce a page that contains the following link:

When the user clicks the text Click Me! in the page, the user's browser sends a request for myscript.pl back to the Web server. Of course, in and of itself, all that will do is cause the script to send out the same page again because no other information is supplied in the URL. However, if you add a parameter to it, that parameter is sent back to the Web server when the user selects the link. The server invokes the script and the script can call param() to detect that the parameter was set and take action according to its value.

To attach a parameter to the end of the URL, add a '?' character followed by a name=value pair indicating the parameter name and its value. For example, to add a size parameter with a value of large, write the URL as follows:

/cgi-bin/myscript.pl?size=large

To attach multiple parameters, separate them by ';' characters:[1]

[1] CGI.pm also understands '&' as a parameter separator character. Other language APIs for Web programming vary in their conventions, so you'll need to know whether they expect ';' or '&' and construct URLs accordingly.

/cgi-bin/myscript.pl?size=large;color=blue

To construct a self-referencing URL with attached parameters, a script should begin by calling the url() function to obtain its own URL, and then append parameters to it as follows:

$url = url (); # get URL for script
$url .= "?size=large"; # add first parameter
$url .= ";color=blue"; # add second parameter

Using url() to get the script path allows you to avoid hardwiring the path into the code.

To generate a hyperlink, pass the URL to CGI.pm's a() function:

print a ({-href => $url}, "Click Me!");

The print statement produces a hyperlink that looks like the following:

The preceding example constructs the value of $url in somewhat cavalier fashion, because it doesn't take into account the possibility that the parameter values or the link label might contain special characters. Unless you're certain that the values and the label don't require any encoding, it's best to use the CGI.pm encoding functions. The escape() function encodes values to be appended to a URL, and escapeHTML() encodes regular HTML text. For example, if the value of the hyperlink label is stored in $label, and the values for the size and color parameters are stored in the variables $size and $color, you can perform the proper encoding as follows:
Previous  Next  
Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist